33

I want to build a JSON Object similar to following the structure in java using JSONObject and JSONArray.

I have gone through various posts in stack overflow, which suggests using methods like push, put etc which I am unable to identify for JSONArray. Please help.

{
    "name": "sample",
    "def": [
        {
            "setId": 1,
            "setDef": [
                {
                    "name": "ABC",
                    "type": "STRING"
                },
                {
                    "name": "XYZ",
                    "type": "STRING"
                }
            ]
        },
        {
            "setId": 2,
            "setDef": [
                {
                    "name": "abc",
                    "type": "STRING"
                },
                {
                    "name": "xyz",
                    "type": "STRING"
                }
            ]
        }
    ]
}

2 Answers 2

69

With the imports org.json.JSONArray and org.json.JSONObject

JSONObject object = new JSONObject();
object.put("name", "sample");
JSONArray array = new JSONArray();

JSONObject arrayElementOne = new JSONObject();
arrayElementOne.put("setId", 1);
JSONArray arrayElementOneArray = new JSONArray();

JSONObject arrayElementOneArrayElementOne = new JSONObject();
arrayElementOneArrayElementOne.put("name", "ABC");
arrayElementOneArrayElementOne.put("type", "STRING");

JSONObject arrayElementOneArrayElementTwo = new JSONObject();
arrayElementOneArrayElementTwo.put("name", "XYZ");
arrayElementOneArrayElementTwo.put("type", "STRING");

arrayElementOneArray.put(arrayElementOneArrayElementOne);
arrayElementOneArray.put(arrayElementOneArrayElementTwo);

arrayElementOne.put("setDef", arrayElementOneArray);
array.put(arrayElementOne);
object.put("def", array);

I did not include first array's second element for clarity. Hope you got the point though.

EDIT:

The previous answer was assuming you were using org.json.JSONObject and org.json.JSONArray.

For net.sf.json.JSONObject and net.sf.json.JSONArray :

JSONObject object = new JSONObject();
object.element("name", "sample");
JSONArray array = new JSONArray();

JSONObject arrayElementOne = new JSONObject();
arrayElementOne.element("setId", 1);
JSONArray arrayElementOneArray = new JSONArray();

JSONObject arrayElementOneArrayElementOne = new JSONObject();
arrayElementOneArrayElementOne.element("name", "ABC");
arrayElementOneArrayElementOne.element("type", "STRING");

JSONObject arrayElementOneArrayElementTwo = new JSONObject();
arrayElementOneArrayElementTwo.element("name", "XYZ");
arrayElementOneArrayElementTwo.element("type", "STRING");

arrayElementOneArray.add(arrayElementOneArrayElementOne);
arrayElementOneArray.add(arrayElementOneArrayElementTwo);

arrayElementOne.element("setDef", arrayElementOneArray);
object.element("def", array);

Basically it's the same, replacing the method 'put' for 'element' in JSONObject, and 'put' for 'add' in JSONArray.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your time, as I mentioned in my question I am unable to access put method of JSONArray. I am using net.sf.Json API to create this objects (JSONObject and JSONArray). Correct me If I am wrong.
I assumed you were using org.json.JSONObject and org.json.JSONArray. Sorry. According to json-lib.sourceforge.net/apidocs/net/sf/json/JSONObject.html and json-lib.sourceforge.net/apidocs/net/sf/json/JSONArray.html, i guess you could replace all 'put' methods for the 'element' method for JSONObject, and replace 'put' for 'add' in JSONArray
0

Here is one crude example. You should be able to refine. (You may be interested in this Java "tutorial" http://docs.oracle.com/javaee/7/tutorial/doc/jsonp.htm#GLRBB

(This example uses the JSON reference implementation included in Java EE (and available here: https://java.net/projects/jsonp/downloads/directory/ri)

package com.demo;
import java.io.FileWriter;
import java.io.IOException;
import javax.json.Json;
import javax.json.stream.JsonGenerator;

public class JSONExample {
public static void main(String[] args) {
    FileWriter writer = null;
    try {
        writer = new FileWriter("C:\\Users\\Joseph White\\Downloads\\jsontext.txt");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    JsonGenerator gen = Json.createGenerator(writer);

    gen.writeStartObject().write("name", "sample")
        .writeStartArray("def")
          .writeStartObject().write("setId", 1)
             .writeStartArray("setDef")
                .writeStartObject().write("name", "ABC").write("type", "STRING")
                .writeEnd()
                .writeStartObject().write("name", "XYZ").write("type", "STRING")
                .writeEnd()
            .writeEnd()
          .writeEnd()
            .writeStartObject().write("setId", 2)
              .writeStartArray("setDef")
                .writeStartObject().write("name", "abc").write("type", "STRING")
                .writeEnd()
                .writeStartObject().write("name", "xyz").write("type", "STRING")
                .writeEnd()
              .writeEnd()
            .writeEnd()
          .writeEnd()
        .writeEnd();

    gen.close();

}

 }

4 Comments

This looks crazy and I bet it's a blast to debug.
This is an overkill.
not looks enterprise standards code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.