【踩坑】com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0, fastjson

踩坑记录
首先,这是一段json

String response = "{
	"code": 0,
	"data": "[{\"id\":\"5a9e1e9911bf23bf9ec69b27fe5fdfd2\",\"status\":\"new\"},{\"id\":\"568d03c910dd77f16941fa6daf747296\",\"status\":\"new\"},{\"id\":\"2f65637c3e09c92aac19d87d8ed2ee08\",\"status\":\"new\"}]"
}";

其次这是我的解析代码

JSONObject jsonObject = JSONObject.parseObject(response);
String data = String.valueOf(jsonObject.get("data"));
StatusCase statusCase = JSONObject.parseObject(data, StatusCase.class);
String securityId = statusCase.getSecurityId();
System.out.println(securityId);

然而报错了

Exception in thread "main" com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0, fastjson-version 1.2.61
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:489)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1539)
	at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_StatusCase.deserialze(Unknown Source)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:284)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:682)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:383)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:287)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:560)

注意分析data里面的数据其实不是JSON
而是数组。
修改代码

JSON.parse(data)去除转译
再解析

 JSONObject jsonObject = JSONObject.parseObject(response);
 String data = String.valueOf(jsonObject.get("data"));
 List<StatusCase> statusCaseList = JSONObject.parseArray(String.valueOf(JSON.parse(data)), StatusCase.class);

如果不去除转译的data
直接parse是parse不出来值的。

null
null
null

你可能感兴趣的:(Java,java,json)