lyhuo
薄荷加冰丶
采纳率80%
2019-11-05 14:10

解析JSON的操作,多层JSON

10
已采纳

对JSON的key做处理;

需要把Key解析出来,拼起来做参数使用

解析后如下:data_是我自己加的,后面的按key的层级,进行拼接

1.value是单层的

data_brand,

data_category,

data_ip,......

2.value是多层的

data_unresolvedData_macAddress,

data_unresolvedData_system_systemBit

JSON如下:

{
    "brand": "server",
    "category": "server",
    "iP": "127.0.0.1",
    "isSave": "true",
    "isSaveRelation": "false",
    "localPort": "",
    "name": "test-app",
    "portID": "",
    "relations": {},
    "type": "server",
    "unresolvedData": {
        "macAddress": "1234567",
        "system": {
            "systemBit": "123",
            "systemType": "123",
            "UUID": "",
            "version": "123"
        },
        "ip": "127.0.0.1",
        "hardDisk": {
            "totalSize": "1GB",
            "usedSize": "1GB",
            "useableSize": "1GB"
        },
        "cpu": {
            "model": "qwertyp",
            "threadNum": "421",
            "cpuNum": "22",
            "totalCoreNum": "412",
            "coreNum": "22"
        },
        "subType": "test",
        "RAM": {
            "totalSlot": "",
            "maxCapacity": "1GB",
            "maximumCapacity": ""
        }
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • CSDN1642816671 发光的树 2年前

    方法

    public static List parseJson(String str,JSONObject json,List list){
            Iterator<String> iterator =  json.keys();
            while(iterator.hasNext()){
                String key = iterator.next();
                String value = json.getString(key);
                JSONObject child = null;
                try {
                    child = JSONObject.fromObject(value);
                    parseJson(str + "_" + key,child,list);
                } catch (Exception e) {
                    list.add(str + "_" + key);
                }
            }
            return list;
        }
    

    调用

    String str = "你的json";
    JSONObject json = JSONObject.fromObject(str);
            List list = new ArrayList();
            parseJson("data",json,list);
            for(int i=0;i<list.size();i++){
                System.out.println(list.get(i));
            }
    
    点赞 评论 复制链接分享
  • tiankongzhichenglyf tkzc_shark 2年前

    1.这样传参有点另类,直接传递json字符串参数过去解析
    2.拼接的话只能一边解析,一边拼接参数

                String url = "xxx.com";
                String json = "{\n" +
                        "    \"brand\": \"server\",\n" +
                        "    \"category\": \"server\",\n" +
                        "    \"iP\": \"127.0.0.1\",\n" +
                        "    \"isSave\": \"true\",\n" +
                        "    \"isSaveRelation\": \"false\",\n" +
                        "    \"localPort\": \"\",\n" +
                        "    \"name\": \"test-app\",\n" +
                        "    \"portID\": \"\",\n" +
                        "    \"relations\": {},\n" +
                        "    \"type\": \"server\",\n" +
                        "    \"unresolvedData\": {\n" +
                        "        \"macAddress\": \"1234567\",\n" +
                        "        \"system\": {\n" +
                        "            \"systemBit\": \"123\",\n" +
                        "            \"systemType\": \"123\",\n" +
                        "            \"UUID\": \"\",\n" +
                        "            \"version\": \"123\"\n" +
                        "        },\n" +
                        "        \"ip\": \"127.0.0.1\",\n" +
                        "        \"hardDisk\": {\n" +
                        "            \"totalSize\": \"1GB\",\n" +
                        "            \"usedSize\": \"1GB\",\n" +
                        "            \"useableSize\": \"1GB\"\n" +
                        "        },\n" +
                        "        \"cpu\": {\n" +
                        "            \"model\": \"qwertyp\",\n" +
                        "            \"threadNum\": \"421\",\n" +
                        "            \"cpuNum\": \"22\",\n" +
                        "            \"totalCoreNum\": \"412\",\n" +
                        "            \"coreNum\": \"22\"\n" +
                        "        },\n" +
                        "        \"subType\": \"test\",\n" +
                        "        \"RAM\": {\n" +
                        "            \"totalSlot\": \"\",\n" +
                        "            \"maxCapacity\": \"1GB\",\n" +
                        "            \"maximumCapacity\": \"\"\n" +
                        "        }\n" +
                        "    }\n" +
                        "}";
                JSONObject jsonObject = JSON.parseObject(json);
                url += "?data_brand=" + jsonObject.get("brand").toString();
                url += "&data_category=" + jsonObject.get("category").toString();
                JSONObject jsonObject2 = JSON.parseObject(jsonObject.get("unresolvedData").toString());
                url += "&data_unresolvedData_macAddress=" + jsonObject2.get("macAddress").toString();
    
    点赞 评论 复制链接分享

为你推荐