代码:
package com;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonTest {
public static String json = "{\"snssaiCrossProvinceIs\":\"0\",\"snssai\":\"128-1c122e\",\"relatedSnssai\":\"128-03FFFF\",\"generalIs\":\"0\",\"provinceNsInfoList\":[{\"subBBOSSOrderId\":\"KC0000020210926100709224741\",\"provinces\":[\"6300\"],\"crossProvinceIs\":\"0\",\"serviceProfile\":{\"maxNumberofUEs\":\"\",\"numofNewCards\":{\"provinceCode\":\"6300\",\"account\":\"10\"},\"anResourceInfo\":{\"anIsolationLevel\":\"1\",\"A6Is\":\"1\",\"PRBInfo\":{\"bindWidth\":\"1\",\"PRBType\":\"1\",\"PRBReserve\":\"1\"},\"A7Is\":\"1\",\"frame\":\"1\"},\"nsType\":\"\",\"coverageArea\":\"[{\\\"coverage\\\":[{\\\"longitude\\\":\\\"250\\\",\\\"latitude\\\":\\\"250\\\"}],\\\"zoneCode\\\":\\\"6301\\\"}]\"},\"pLMNIdList\":[\"460-00\"]}],\"interRoamingIs\":\"0\"}";
static Map<Object, Object> attr = new HashMap<>();
public static void jsonLoop(Object object) {
if(object instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) object;
Set<Map.Entry<String, Object>> es= jsonObject.entrySet();
for (Map.Entry<String, Object> entry : es) {
Object o = entry.getValue();
if(o instanceof String) {
attr.put(entry.getKey(),entry.getValue());
System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
} else {
jsonLoop(o);
}
}
}
if(object instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) object;
for(int i = 0; i < jsonArray.size(); i ++) {
jsonLoop(jsonArray.get(i));
}
}
}
public static void main(String[] args) {
JSONObject jsonObject = JSONObject.fromObject(json);
jsonLoop(jsonObject);
System.out.println(attr.toString());
}
}