Reputation: 125
I trying to create the code which generates the JSON data which should look like this
{
"Main": [
{
"prim": "Hello ",
"secon": [
{
"ads": "A Message"
}
]
}
]
}
The code I am trying with is generating like below
{
"Main": [
{
"prim": "Hello"
},
{
"secon": [
{
"ads": "A Message"
}
]
}
]
}
Code:
JSONObject prim = new JSONObject();
prim.put("prim", Hello");
JSONObject ads = new JSONObject();
ads.put("ads", "A Message");
JSONArray seconArray = new JSONArray();
seconArray.put(ads);
JSONObject secon = new JSONObject();
secon.put("secon", seconArray);
JSONArray Main = new JSONArray();
Main.put(prim);
Main.put(secon);
JSONObject jsonObj = new JSONObject();
jsonObj.put("Main", Main);
JSONArray topJson = new JSONArray();
topJson.put(jsonObj);
System.out.println(topJson.get(0).toString());
How to remove the unnecessary brackets and create the intended Json data?
Upvotes: 0
Views: 44
Reputation: 11028
Instead of:
secon.put("secon", seconArray);
Try:
prim.put("secon", seconArray);
And remove:
Main.put(secon);
Upvotes: 3