简体   繁体   English

如何在Java中解析二维JSON对象

[英]How to parse a two dimensional JSON Object in java

This is my JSON data 这是我的JSON数据

{

    "field1" : [
        {
            "key1" : "1",
            "key2" : "2"
        }, {
            "key1" : "1",
            "key2" : "2",
            "key3" : "3",
            "key4" : "4"
        }
    ],
    "field2" : {
        "key1" : "1",
        "key2" : "2",
        "key3" : "3",
        "key4" : "4",
        "key5" : "5"
    },
    "field3" : {
        "key1" : "1"
    }
}

this is my code 这是我的代码

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

Set arr = jsonObject.keySet();
Iterator iterator = arr.iterator();
Collection innerArray = jsonObject.values();
Iterator iterator1 = innerArray.iterator();
while (iterator.hasNext() && iterator1.hasNext()) {
    System.out.println("key="+iterator.next().toString());
    System.out.println("value="+ iterator1
            .next().toString());
}

i need output like this 我需要这样的输出

field1
-------
key1 -->1

key2 -->2

field1
----------
key1 -->1

key2 -->2

key3 -->3

key4 -->4


field2
--------
key1 -->1

key2 -->2 

key3 -->3

key4 -->4

key5 -->5


field3
------
key1 --> 1

Current Output: 电流输出:

key=field3 value={"key1":"1"} 

key=field2 value={"key4":"4","key3":"3","key5":"5","key2":"2","key1":"1"}

key=field1 value=[{"key2":"2","key1":"1"},{"key4":"4","key3":"3","key2":"2","key1":"1"}] 

Any idea? 任何想法?

You can make few alterations for printing exactly how you need... below code should help. 您可以进行一些更改,以完全按照所需的方式打印...下面的代码应该会有所帮助。 This code is generic and can be used for any JSON structure.. 此代码是通用代码,可用于任何JSON结构。

static void printRecursive(JSONObject obj) {
    for(Object key:obj.keySet()) {

        //System.out.println(obj.get(key.toString()).getClass().getSimpleName());
        if(obj.get(key.toString()) instanceof JSONArray) {
            JSONArray aobj = ((JSONArray)obj.get(key.toString()));
            System.out.println(key.toString());
            for(int i=0;i<aobj.length();i++) {
                printRecursive(aobj.getJSONObject(i));
            }
        }
        else
        if(obj.get(key.toString()) instanceof JSONObject) {
            System.out.println(key.toString());
            printRecursive((JSONObject)obj.get(key.toString()));
        }
        else
            System.out.println(key.toString()+" -> "+obj.get(key.toString()));
    }
}

You need to add a method that can be used recursively to check if a value is a JSONObject or JSONArray . 您需要添加一种可以递归使用的方法,以检查值是JSONObject还是JSONArray

For Example: 例如:

public void outputResultsOfJson(String parentKey, Object obj) {
    if (obj instanceof JSONObject) {
        System.out.println(parentKey + " is an Object");
        JSONObject jObj = (JSONObject)obj;
        Iterator<?> keys = jObj.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            outputResultsOfJson(key, jObj.get(key));
        }
    } else if (obj instanceof JSONArray) {
        System.out.println(parentKey + " is an Array");
        JSONArray jArr = (JSONArray)obj;
        for (int i = 0; i < jArr.length(); i++) {
            outputResultsOfJson("#" + i, jArr.get(i));
        }
    } else {
        System.out.println(parentKey + " is a String");
    }
}

This function will check for the type of each key in an object or index in an array and output its type (Object, Array, or String). 该函数将检查对象或数组中索引中每个键的类型,并输出其类型(对象,数组或字符串)。 Adapt it to your needs. 使其适应您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM