问题遇到的现象和发生背景
从远端获取JSON数据时,发现转换不了
问题相关代码,请勿粘贴截图
这是返回的body示例
[
"{"Id": "LM1", "Type": 0, "Name": "LocationMark0", "Posture": [-0.589, -0.696, 0], "Property": [{"key": "spin", "type": "bool", "value": "ZmFsc2U=", "boolValue": false}]}",
"{"Id": "LM2", "Type": 0, "Name": "LocationMark1", "Posture": [5.836, -0.321, 0], "Property": [{"key": "spin", "type": "bool", "value": "ZmFsc2U=", "boolValue": false}]}"
]
@Test
void getLandMark() {
//result是用UTF8转换的结果 result就是上面的数组
String result = HttpClientUtils.doGet(apiProperties.getMapInfourl(), apiProperties.getQueryLandmarks(), null);
List<LandMark> landMarks;
if (StringUtils.isEmpty(result)) {
//去除转义符未错,实体类转换错了(但是我惊奇的发现,去除转义符前后没变)
landMarks = JSONObject.parseArray(String.valueOf(JSON.parse(result)), LandMark.class);
System.out.println(landMarks);
}
}
我的实体类是这样子的
@Data
public class LandMark implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private List<Double> posture;
private Integer type;
private List<Property> property;
}
@Data
public class Property implements Serializable {
private static final long serialVersionUID = 1L;
private String key;
private String value;
private String type;
private boolean boolValue;
}
运行结果及报错内容
com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:233)
at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseRest(ASMJavaBeanDeserializer.java:101)
at Fastjson_ASM_LandMark_1.deserialze(Unknown Source)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:556)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:251)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:227)
我的解答思路和尝试过的方法
我想要达到的结果
按照实体类获取 如果不能按照实体类获取,那就想个办法获取,我自己转实体类也行