0

I need to generate a JSON array object ("MainData") as shown below using Java. Can any one suggest to me how can be this done?

{
  "MainData":{
    "columnHeaderKeys":null,
    "rowHeaders":[
      {
        "id":0001,
        "name":abcd
      },
      {
        "id":0002,
        "name":xyz
      }
    ],
    "data":[
      {
        "id":0001,
        "rowId":"R1",
        "status":PASSED
      },
      {
        "id":0002,
        "rowId":"R2",
        "status":PASSED
      }
    ]
  }
}
2

4 Answers 4

3

Using Google GSON, it's quite simple:

JSONObject result = new JSONObject();
result.put("columnHeadersKeys", JSONObject.NULL);

JSONArray headers = new JSONArray();

JSONObject obj = new JSONObject();
obj.put("id", 1);
obj.put("name", "abcd");
headers.put(obj);

obj = new JSONObject();
obj.put("id", 2);
obj.put("name", "xyz");
headers.put(obj);

result.put("rowHeaders", headers);

JSONArray data = new JSONArray();

obj = new JSONObject();
obj.put("id", 1);
obj.put("rowId", "R1");
obj.put("status", "PASSED");
data.put(obj);

obj = new JSONObject();
obj.put("id", 2);
obj.put("rowId", "R2");
obj.put("status", "PASSED");
data.put(obj);

result.put("data", data);

String output = result.toString();

Note that the entire creation of the object can be chained in one statement - however I find it easier to read when it's split out.

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

Comments

2

Use the Jackson library.

A sample code from the tutorial:

ObjectMapper mapper = new ObjectMapper();

Map<String,Object> userData = new HashMap<String,Object>();
Map<String,String> nameStruct = new HashMap<String,String>();
nameStruct.put("first", "Joe");
nameStruct.put("last", "Sixpack");
userData.put("name", nameStruct);
userData.put("gender", "MALE");
userData.put("verified", Boolean.FALSE);
userData.put("userImage", "Rm9vYmFyIQ==");

mapper.writeValue(new File("user-modified.json"), userData);

1 Comment

whoops, forget the mapper! fixing it.
0

You can do like this It works perfectly for me with this line Log.i("MainJSON", MainJSON.toString()); I have printed Created JSONObject

try{
        JSONObject jo1=new JSONObject();
        jo1.put("id",001);
        jo1.put("name","abcd");

        JSONObject jo2=new JSONObject();
        jo2.put("id",002);
        jo2.put("name","xyz");

        JSONArray jarr1=new JSONArray();
        jarr1.put(0, jo1);
        jarr1.put(1, jo2);

        JSONObject jo3=new JSONObject();
        jo3.put("id",0001);
        jo3.put("rowId","R1");
        jo3.put("status","PASSED");

        JSONObject jo4=new JSONObject();
        jo4.put("id",0002);
        jo4.put("rowId","R2");
        jo4.put("status","PASSED");

        JSONArray jarr2=new JSONArray();
        jarr2.put(0, jo3);
        jarr2.put(1, jo4);

        JSONObject MainDataObj=new JSONObject();
        MainDataObj.put("rowHeaders", jarr1);
        MainDataObj.put("data", jarr2);
        MainDataObj.put("columnHeaderKeys", "null");

        JSONObject MainJSON=new JSONObject();
        MainJSON.put("MainData", MainDataObj);

        Log.i("MainJSON", MainJSON.toString());
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

Comments

0

Take a look here. You will find many projects that will help you.

I would try JSONObject.java from Crockford's JSON for Java Github repository.

Simple usage :

JSONObject obj = new JSONObject()
    .put("MainData", new JSONObject()
        .put("columnHeaderKeys", null)
        .put("rowHeaders", new JSONArray()
            .put(new JSONObject()
                .put("id", "001")
                .put("name", "abc")
            )
            .put(new JSONObject()
                .put("id", "002")
                .put("rowId", "R1")
            // ...
        )
    );

Output string with :

String jsonString = obj.toString();

or write directly to file :

obj.write(new FileWriter(new File("/path/to/destination/file")));

3 Comments

stackoverflow.com/questions/how-to-answer I believe you already know this, however please look at "Provide context for links" paragraph. Otherwise it should rather be a helpful comment.
When I was writing that comment the answer looked like Take a look here. You will find many projects that will help you. Now it's merely better. C'mon, you are here long enough. Just look at @elias answer.
I could do the JSON creation successfully. Thank you all for your timely help and support.

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.