Java Json字符串解析
作者:互联网
这里使用fastjson来进行解析,fastjson里有两个重要的对象JSONObject和JSONArray。看下面的json字符串,meta、data属于JSONObject,data子集因为有[ ]所以子集属于JSONArray。
1 { 2 "meta": { 3 "page": 1, 4 "limit": 10000, 5 "hasnext": false, 6 "count": 1 7 }, 8 "data": [ 9 { 10 "recv_cnt": 110, 11 "proto_name": "MQTT", 12 "clientid": "mqttx_7c2fecaa", 13 "connected_at": "2020-11-29 12:28:18", 14 "mailbox_len": 0, 15 "send_pkt": 111, 16 "recv_oct": 294, 17 "max_mqueue": 1000, 18 "send_msg": 2, 19 "connected": true, 20 "mountpoint": "undefined", 21 "max_subscriptions": 0, 22 "heap_size": 610, 23 "subscriptions_cnt": 1, 24 "zone": "external", 25 "max_awaiting_rel": 100, 26 "mqueue_dropped": 0, 27 "is_bridge": false, 28 "expiry_interval": 0, 29 "recv_pkt": 3, 30 "node": "emqx@127.0.0.1", 31 "mqueue_len": 0, 32 "ip_address": "192.168.3.3", 33 "proto_ver": 4, 34 "inflight": 0, 35 "keepalive": 60, 36 "reductions": 87838, 37 "created_at": "2020-11-29 12:28:18", 38 "send_cnt": 111, 39 "awaiting_rel": 0, 40 "username": "undefined", 41 "clean_start": true, 42 "send_oct": 276, 43 "port": 53482, 44 "recv_msg": 1, 45 "max_inflight": 32 46 } 47 ], 48 "code": 0 49 }
1.获取code字段值。
String jsonString = "{"meta":{"page":1,"limit":10000,"hasnext":false,"count":1},"data":[{"recv_cnt":110,"proto_name":"MQTT","clientid":"mqttx_7c2fecaa","connected_at":"2020-11-29 12:28:18","mailbox_len":0,"send_pkt":111,"recv_oct":294,"max_mqueue":1000,"send_msg":2,"connected":true,"mountpoint":"undefined","max_subscriptions":0,"heap_size":610,"subscriptions_cnt":1,"zone":"external","max_awaiting_rel":100,"mqueue_dropped":0,"is_bridge":false,"expiry_interval":0,"recv_pkt":3,"node":"emqx@127.0.0.1","mqueue_len":0,"ip_address":"192.168.3.3","proto_ver":4,"inflight":0,"keepalive":60,"reductions":87838,"created_at":"2020-11-29 12:28:18","send_cnt":111,"awaiting_rel":0,"username":"undefined","clean_start":true,"send_oct":276,"port":53482,"recv_msg":1,"max_inflight":32}],"code":0}";
JSONObject outJson = JSONObject.parseObject(jsonString);
System.out.println(outJson.getIntValue("code"));
2.获取meta下的“limit“字段值。
String jsonString = "{"meta":{"page":1,"limit":10000,"hasnext":false,"count":1},"data":[{"recv_cnt":110,"proto_name":"MQTT","clientid":"mqttx_7c2fecaa","connected_at":"2020-11-29 12:28:18","mailbox_len":0,"send_pkt":111,"recv_oct":294,"max_mqueue":1000,"send_msg":2,"connected":true,"mountpoint":"undefined","max_subscriptions":0,"heap_size":610,"subscriptions_cnt":1,"zone":"external","max_awaiting_rel":100,"mqueue_dropped":0,"is_bridge":false,"expiry_interval":0,"recv_pkt":3,"node":"emqx@127.0.0.1","mqueue_len":0,"ip_address":"192.168.3.3","proto_ver":4,"inflight":0,"keepalive":60,"reductions":87838,"created_at":"2020-11-29 12:28:18","send_cnt":111,"awaiting_rel":0,"username":"undefined","clean_start":true,"send_oct":276,"port":53482,"recv_msg":1,"max_inflight":32}],"code":0}";
JSONObject outJson = JSONObject.parseObject(jsonString); //先将原始json字符串转换成JSONObject
String metaString = outJson.getString("meta"); //从JSONObject中获取meta字符串
JSONObject metaObj = JSONObject.parseObject(metaString); //再将meta字符串转换成JSONObject
System.out.println(metaObj.getIntValue("limit")); //获取limit字段值
3.获取data下的clientid字段值。
String jsonString = "{"meta":{"page":1,"limit":10000,"hasnext":false,"count":1},"data":[{"recv_cnt":110,"proto_name":"MQTT","clientid":"mqttx_7c2fecaa","connected_at":"2020-11-29 12:28:18","mailbox_len":0,"send_pkt":111,"recv_oct":294,"max_mqueue":1000,"send_msg":2,"connected":true,"mountpoint":"undefined","max_subscriptions":0,"heap_size":610,"subscriptions_cnt":1,"zone":"external","max_awaiting_rel":100,"mqueue_dropped":0,"is_bridge":false,"expiry_interval":0,"recv_pkt":3,"node":"emqx@127.0.0.1","mqueue_len":0,"ip_address":"192.168.3.3","proto_ver":4,"inflight":0,"keepalive":60,"reductions":87838,"created_at":"2020-11-29 12:28:18","send_cnt":111,"awaiting_rel":0,"username":"undefined","clean_start":true,"send_oct":276,"port":53482,"recv_msg":1,"max_inflight":32}],"code":0}";
JSONObject outJson = JSONObject.parseObject(jsonString);
String dataString = outJson.getString("data");
JSONArray dataArray = JSONObject.parseArray(dataString);
for (int i = 0; i < dataArray.size(); i++) {
String clientid = dataArray.getJSONObject(i).getString("clientid");
System.out.println(clientid);
}
总结:
1.如果字段是单值,那么原始json字符串转换为JSONObject后直接获取;
2.如果字段是对象,那么需要先获取字段的String再转换为JSONObject,如此循环直到获取到你想要的字段;
3.如果字段是数组,那么需要先将其转换为JSONArray,然后再套用第1和2点。
标签:cnt,Java,max,mqueue,send,Json,JSONObject,字符串,recv 来源: https://www.cnblogs.com/kmpro/p/14091604.html