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?
Instead of:
secon.put("secon", seconArray);
Try:
prim.put("secon", seconArray);
And remove:
Main.put(secon);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.