0

I have the following JSON structure:

{
    "PARAMORDER": [{
        "TAB1": [{
            "1": "Picture ID Source"
        }, {
            "2": "Place of Issuance"

        }],
        "TAB2": [{
            "1": "Picture ID Source"
        }, {
            "2": "Place of Issuance"

        }]
    }]
}

I am trying to create a JSON Array using java code which looks like the above format when it is parsed and retrieved. I am using org.json.simple API for this. However I am unable to create an array of array in JSON using java code. Can someone please share me a sample code which can construct the JSON in the above format.

Below is the sample code I tried which creates a json array:

JSONArray jsonArray = new JSONArray();
JSONObject firstJson = new JSONObject();
JSONObject secondJson = new JSONObject();

firstJson.put("1", "Picture ID Source");
secondJson.put("1", "Picture ID Source");

jsonArray.add(firstJson);
jsonArray.add(secondJson);

System.out.println(jsonArray.toString);

This gives me the following JSON:

[{
    "1": "Picture ID Source"
}, {
    "1": "Picturesecond ID Source"
}]

I am unable to create a JSONArray of JSONArray. Can someone please help me with that? Thanks in Advance.

2
  • First of all, we are not a question-and-answer site, code-writing service. Second of all, that code does not produce that JSON. Commented Feb 28, 2017 at 6:23
  • I knew that code does not give the JSON which I need. I just wanted some suggestion like just a sample code to construct it and that is why I just posted here. Stack Over Flow has always helped me to enhance my skills and I also knew this is not a code-writing service. Commented Feb 28, 2017 at 21:03

1 Answer 1

1

You're on the right track, but you need a lot more code to create the intermediate levels, the structures can be added in a tree-like manner indefinitely. Also your top level in your sample is a JSON object, not an array.

JSONObject root = new JSONObject();
JSONArray paraArray = new JSONArray();
JSONObject a = new JSONObject();
JSONArray tab1 = new JSONArray();
JSONObject source1 = new JSONObject();
source1.put("1", "Picture ID Source");
tab1.add(source1);
JSONObject source2 = new JSONObject();
source2.put("2", "Place of Issuance");
tab1.add(source2);
a.put("TAB1", tab1);
paraArray.add(a);

JSONObject b = new JSONObject();
JSONArray tab2 = new JSONArray();
JSONObject source3 = new JSONObject();
source3.put("1", "Picture ID Source");
tab2.add(source3);
JSONObject source4 = new JSONObject();
source4.put("2", "Place of Issuance");
tab2.add(source4);
b.put("TAB2", tab2);
paraArray.add(b);

root.put("PARAMORDER", paraArray);

System.out.println(root.toString());

Output

{"PARAMORDER":[{"TAB1":[{"1":"Picture ID Source"},{"2":"Place of Issuance"}]},{"TAB2":[{"1":"Picture ID Source"},{"2":"Place of Issuance"}]}]}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Adam !! That idea has solved my problem :) Thanks again !!

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.