如何在java

时间:2015-07-29 14:23:21

标签: java json

这是我的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"
    }
}

这是我的代码

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());
}

我需要这样的输出

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

当前输出:

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"}] 

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您可以根据需要对打印进行少量更改...以下代码应该有所帮助。此代码是通用的,可用于任何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()));
    }
}

答案 1 :(得分:1)

您需要添加一个可以递归使用的方法来检查值是JSONObject还是JSONArray

例如:

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");
    }
}

此函数将检查数组中对象或索引中每个键的类型,并输出其类型(Object,Array或String)。根据您的需求进行调整。