compile 'com.alibaba:fastjson:1.2.31'
compile 'com.alibaba:fastjson:1.1.57.android'
有如下json字符串和类:
String t2 = "{\"state\":2,\"msg\":\"msg2222\",\"data\":{}}";
public static class Test {
public int state;
public String msg;
public List<Data> data;
public static class Data {
public int d1;
public String d2;
}
}
测试代码如下:
try {
Test c2 = JSON.parseObject(t2, Test.class);
Log.d("JSON", "c2 ----");
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(t2);
Test model = jsonObject.toJavaObject(Test.class);
Log.d("JSON", "model ----");
} catch ( Exception e) {
e.printStackTrace();
}
c2可以格式化出结果,但结果是错误的, c2 = {MainActivity$Test@830029442312} data = {ArrayList@830029442520} size = 1 0 = {MainActivity$Test$Data@830029442600} d2 = null d1 = 0 msg = "msg2222" state = 2 model执行出现异常 e = {JSONException@830029375048} "com.alibaba.fastjson.JSONException: can not cast to : java.util.List<com.yyw.fastjson_example.MainActivity$Test$Data>" cause = {JSONException@830029373272} "com.alibaba.fastjson.JSONException: can not cast to : java.util.List<com.yyw.fastjson_example.MainActivity$Test$Data>" detailMessage = "can not cast to : java.util.List<com.yyw.fastjson_example.MainActivity$Test$Data>" stackState = {int[38]@830029375080} stackTrace = {StackTraceElement[0]@830024963328} suppressedExceptions = {Collections$EmptyList@830024961208} size = 0