0

This is my JSON data

{

    "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?

brso05
  • 13,142
  • 2
  • 21
  • 40
Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95

2 Answers2

2

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..

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()));
    }
}
Nandan P
  • 106
  • 4
1

You need to add a method that can be used recursively to check if a value is a JSONObject or 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.

4mnes7y
  • 158
  • 8