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?

10
  • 3
    So what is your current output? It's not clear at the moment which part you're having trouble with. Commented Jul 29, 2015 at 14:27
  • 1
    possible duplicate of How to parse JSON in Java Commented Jul 29, 2015 at 14:32
  • sorry, iam a beginner in stackoverflow. Commented Jul 29, 2015 at 14:32
  • @AbdulManaf it's alright no worries man Commented Jul 29, 2015 at 14:33
  • 1
    Answer depends on if this JSON format fixed. If there can be more levels of nesting you would probably need recursion, but if format is fixed you can simply check if element is array or not. If it is not, print all key-value pairs, if it is, you will need to add another iterator to print over elements of array. Commented Jul 29, 2015 at 14:42

2 Answers 2

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()));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.