Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"
                }
            ]
        }
    ]
}
share|improve this question
add comment

closed as off-topic by Anders R. Bystrup, Ian Kemp, Lipis, gleng, ArtB Feb 26 at 14:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself." – Anders R. Bystrup, Ian Kemp, Lipis, gleng, ArtB
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 1 down vote accepted
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);
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.

share|improve this answer
 
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. –  user3131769 Feb 26 at 13:47
 
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 –  goten Feb 26 at 14:01
 
Thank you so much..Its working Now :) –  user3131769 Feb 27 at 9:34
add comment

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

}

 }
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.