本次涉及对JSON字符串进行解析:
1.JSON字符串转JSONObejct
2.从JSONObject中提取需要的节点数据
3.利用JSONArray将JSON字符串转成List
要解析的JSON字符串结构:
1.JSON.parseObject(jsonStr),对json字符串转成json对象,可看到长度为4,里面的结构还是比较清晰明了的
2.拿到jsonObject之后,可以通过obj.getString(key)获取自己需要节点的json字符串,便与后续对这部分进行处理。此处看到我们拿到totalcount为2。接下来想对data进行处理,把它转成list
3.json字符串转List
@Test
public void testJson(){
String jsonStr = "{\n" +
" \"responseHeader\": {\n" +
" \"result\": 1,\n" +
" \"desc\": \"success\",\n" +
" \"headExtensions\": {\n" +
" \"traceId\": \"679a13848b87dfba\"\n" +
" },\n" +
" \"services\": \"S1000\"\n" +
" },\n" +
" \"operationCfg\": {\n" +
" \"userName\": null,\n" +
" \"transactionId\": \"T_EMS\",\n" +
" \"tableName\": \"T_EMS\",\n" +
" \"querytype\": null,\n" +
" \"masterTableName\": null,\n" +
" \"masterTableAliasName\": null\n" +
" },\n" +
" \"totalCount\": null,\n" +
" \"data\": [\n" +
" {\n" +
" \"VENDOR_NAME\": \"WRI\",\n" +
" \"MQ_IP\": \"132.37.99.136:61616\",\n" +
" \"WEBSERVICE_URL\": \"http://132.37.99.136:8086/mtosi/*\",\n" +
" \"INSERT_TIME\": null,\n" +
" \"SOURCE_OBJ_ID\": null,\n" +
" \"DATA_DOMAIN_ID\": \"1000\",\n" +
" \"TRACE_ID\": null,\n" +
" \"DATA_SOURCE\": null,\n" +
" \"ADD_USER\": null,\n" +
" \"USER_NAME\": \"otms2019\",\n" +
" \"UPDATE_TIME\": null,\n" +
" \"EMS_ID\": \"6CF794167CE14E69B85EAD4373FAAF3F\",\n" +
" \"EMS_NAME\": \"烽火ROADM网管\",\n" +
" \"INTERFACE_VERSION\": null,\n" +
" \"MQ_PASSWORD\": \"admin\",\n" +
" \"IP_PORT\": \"8086\",\n" +
" \"USER_PASSWORD\": \"jt_fhROADM@2019\",\n" +
" \"IP_ADDRESS\": \"132.37.99.136\",\n" +
" \"MQ_USER\": \"admin\",\n" +
" \"MODIFY_USER\": null,\n" +
" \"EMS_VERSION\": null,\n" +
" \"VENDOR_OBJ_NAME\": null\n" +
" },\n" +
"{\n" +
" \"VENDOR_NAME\": \"WRI\",\n" +
" \"MQ_IP\": \"132.37.99.136:61616\",\n" +
" \"WEBSERVICE_URL\": \"http://132.37.99.136:8086/mtosi/*\",\n" +
" \"INSERT_TIME\": null,\n" +
" \"SOURCE_OBJ_ID\": null,\n" +
" \"DATA_DOMAIN_ID\": \"1000\",\n" +
" \"TRACE_ID\": null,\n" +
" \"DATA_SOURCE\": null,\n" +
" \"ADD_USER\": null,\n" +
" \"USER_NAME\": \"otms2019\",\n" +
" \"UPDATE_TIME\": null,\n" +
" \"EMS_ID\": \"6CF794167CE14E69B85EAD4373FAAF3F\",\n" +
" \"EMS_NAME\": \"烽火ROADM网管\",\n" +
" \"INTERFACE_VERSION\": null,\n" +
" \"MQ_PASSWORD\": \"admin\",\n" +
" \"IP_PORT\": \"8086\",\n" +
" \"USER_PASSWORD\": \"jt_fhROADM@2019\",\n" +
" \"IP_ADDRESS\": \"132.37.99.136\",\n" +
" \"MQ_USER\": \"admin\",\n" +
" \"MODIFY_USER\": null,\n" +
" \"EMS_VERSION\": null,\n" +
" \"VENDOR_OBJ_NAME\": null\n" +
" }\n" +
" ]\n" +
"}\n";
JSONObject obj = JSON.parseObject(jsonStr);
String data = obj.getString("data");
System.out.println(data);
String totalCount = obj.getString("totalCount");
System.out.println(totalCount);
List<Map> maps = JSONArray.parseArray(data, Map.class);
}