问题描述:在获取json返回值变化的情况下(上层接口返回值定义为:若为一条数据以“{}”返回,若为多条以数组形式返回),在jsonObject还是jsonArray判断上有一定困难。
解决方法1:
直接判断getJsonArray(“xxx”)== null 进行判断
解决结果:gg(类型转换异常)
java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.alibaba.fastjson.JSONArray
解决方法2:
JsonObject object = json.getJsonObject(“xxx”);
判断 object != null && object.size > 0
解决结果:gg(迷之异常)
com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0
解决方法3:
引入jar包
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
Instanceof直接判断JSONObject还是JSONArray;
String storage = jsonObj.getString("storage");
Object storageObject = new JSONTokener(storage).nextValue();
if (storageObject instanceof net.sf.json.JSONObject) {
解决结果:beautiful