简体   繁体   English

JSONObject创建Json对象问题

[英]JSONObject create Json object issue

I trying to create the code which generates the JSON data which should look like this 我试图创建代码来生成应如下所示的JSON数据

{
  "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? 如何删除不必要的括号并创建预期的Json数据?

Instead of: 代替:

 secon.put("secon", seconArray);

Try: 尝试:

 prim.put("secon", seconArray);

And remove: 并删除:

 Main.put(secon);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM