JSON字符串转List,JOSN字符串提取节点并处理

本次涉及对JSON字符串进行解析:
1.JSON字符串转JSONObejct
2.从JSONObject中提取需要的节点数据
3.利用JSONArray将JSON字符串转成List(List)

要解析的JSON字符串结构:
在这里插入图片描述
1.JSON.parseObject(jsonStr),对json字符串转成json对象,可看到长度为4,里面的结构还是比较清晰明了的在这里插入图片描述
2.拿到jsonObject之后,可以通过obj.getString(key)获取自己需要节点的json字符串,便与后续对这部分进行处理。此处看到我们拿到totalcount为2。接下来想对data进行处理,把它转成list
在这里插入图片描述
3.json字符串转List,也可以转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);
		
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值