判断字段是否存在于JSON中并且这个字段是JSONArray

  • 记录下实际开发过程中遇到的问题。报错信息:java.lang.ClassCastException: java.lang.String cannot be cast to com.alibaba.fastjson.JSONArray
    模拟调用返回的数据JSON
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    [
    {
    "code": 200,
    "msg": null,
    "data": {
    "basicForm": [
    {
    "formType": "1",
    "formVersion": "73",
    "emptyJson": {
    "contactMobile": "",
    "address": "",
    "contactCode": "",
    "contactName": "",
    "applyerName": "",
    "contactType": "",
    "applyerAreaCode": "",
    "applyerMobile": "",
    "applyerPageType": "",
    "applyerPageCode": ""
    }
    }
    ],
    "clutterForm": ""
    }
    },
    {
    "code": 200,
    "msg": null,
    "data": {
    "basicForm": [
    {
    "formType": "1",
    "formVersion": "73",
    "emptyJson": {
    "contactMobile": "",
    "address": "",
    "contactCode": "",
    "contactName": "",
    "applyerName": "",
    "contactType": "",
    "applyerAreaCode": "",
    "applyerMobile": "",
    "applyerPageType": "",
    "applyerPageCode": ""
    }
    }
    ],
    "clutterForm": [
    {
    "formVersion": "2",
    "templateId": "960b1dbd-0acc-4626-8a55-15ff0f1962b3",
    "pcJson": {
    "submitBtn": false,
    "emptyText": "清空",
    "column": [
    {
    "arrow": false,
    "display": true,
    "labelWidth": "0px",
    "type": "title",
    "children": {
    "column": []
    },
    "prop": "yhzsx",
    "styles": {
    "color": "#000",
    "fontSize": "18px"
    },
    "value": "原核准事项",
    "collapse": true
    },
    {
    "arrow": false,
    "display": true,
    "labelWidth": "0px",
    "type": "title",
    "children": {
    "column": []
    },
    "prop": "bghdsx",
    "styles": {
    "color": "#000",
    "fontSize": "18px"
    },
    "value": "变更后的事项",
    "collapse": true
    }
    ]
    },
    "taskHandleItemCode": "11230000MB1846112W200011606000401"
    }
    ]
    }
    }
    ]

需求是取出JSON中的”formVersion”的值,这俩条JSON中第一条没有formVersion,而且clutterForm返回的是””,所以要对clutterForm进行校验,实例如下:
注意:在if判断中必须要加上 jsonObject.getJSONObject(data).get(clutterForm) instanceof JSONArray 校验clutterForm是否是一个JSONArray否则会报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 取出JSON中的"formVersion"的值
*/
public static void main(String[] args) {
String json1 = "{\"code\":200,\"msg\":null,\"data\":{\"basicForm\":[{\"formType\":\"1\",\"formVersion\":\"73\",\"emptyJson\":{\"contactMobile\":\"\",\"address\":\"\",\"contactCode\":\"\",\"contactName\":\"\",\"applyerName\":\"\",\"contactType\":\"\",\"applyerAreaCode\":\"\",\"applyerMobile\":\"\",\"applyerPageType\":\"\",\"applyerPageCode\":\"\"}}],\"clutterForm\":\"\"}}";
String json2 = "{\"code\":200,\"msg\":null,\"data\":{\"basicForm\":[{\"formType\":\"1\",\"formVersion\":\"73\",\"emptyJson\":{\"contactMobile\":\"\",\"address\":\"\",\"contactCode\":\"\",\"contactName\":\"\",\"applyerName\":\"\",\"contactType\":\"\",\"applyerAreaCode\":\"\",\"applyerMobile\":\"\",\"applyerPageType\":\"\",\"applyerPageCode\":\"\"}}],\"clutterForm\":[{\"formVersion\":\"2\",\"templateId\":\"960b1dbd-0acc-4626-8a55-15ff0f1962b3\",\"pcJson\":{\"submitBtn\":false,\"emptyText\":\"清空\",\"column\":[{\"arrow\":false,\"display\":true,\"labelWidth\":\"0px\",\"type\":\"title\",\"children\":{\"column\":[]},\"prop\":\"yhzsx\",\"styles\":{\"color\":\"#000\",\"fontSize\":\"18px\"},\"value\":\"原核准事项\",\"collapse\":true},{\"arrow\":false,\"display\":true,\"labelWidth\":\"0px\",\"type\":\"title\",\"children\":{\"column\":[]},\"prop\":\"bghdsx\",\"styles\":{\"color\":\"#000\",\"fontSize\":\"18px\"},\"value\":\"变更后的事项\",\"collapse\":true}]},\"taskHandleItemCode\":\"11230000MB1846112W200011606000401\"}]}}";
List<String> resultList = new ArrayList<>();
resultList.add(json1);
resultList.add(json2);
JSONObject jsonObject = null;
int count = 1;
for (String s : resultList) {
jsonObject = JSON.parseObject(s);
String formVersion = "";
if (jsonObject.containsKey("data")
&& jsonObject.getJSONObject("data").containsKey("clutterForm")
&& jsonObject.getJSONObject("data").get("clutterForm") instanceof JSONArray) {
JSONArray clutterForm = jsonObject.getJSONObject("data").getJSONArray("clutterForm");
if (clutterForm != null && clutterForm.size() > 0) {
JSONObject formObject = clutterForm.getJSONObject(0);
formVersion = formObject.getString("formVersion");
} else {
formVersion = "";
}
}
System.out.println("第" + count + "次version的值:" + formVersion);
count++;
}
}

打印结果:

1
2
3
4
第1次version的值:
第2次version的值:2

Process finished with exit code 0