如何在JsonArray中的某个随机索引处添加JsonObject

如何在JsonArray中的某个随机索引处添加JsonObject,json,jsonobject,Json,Jsonobject,我有一个JSONA数组,如下所示: JsonARray ProfileTabs = [{"totalItemCount":0,"label":"Updates","name":"update"},{"totalItemCount":2,"label":"Members","url":"groups\/member\/list\/129","urlParams":[],"name":"members"},{"totalItemCount":5,"label":"Photos","url":"gro

我有一个JSONA数组,如下所示:

JsonARray ProfileTabs = [{"totalItemCount":0,"label":"Updates","name":"update"},{"totalItemCount":2,"label":"Members","url":"groups\/member\/list\/129","urlParams":[],"name":"members"},{"totalItemCount":5,"label":"Photos","url":"groups\/photo\/list","urlParams":{"group_id":129},"name":"photos"}]
在上面的JsonArray中,我在索引0、1、2处有3个JsonObject

现在我想在这个JsonArray的索引1处添加一个新的JsonObject

因此,我创建了一个新的JsonObject,如下所示:

JSONObject InfoTabJsonObject = new JSONObject();
                    InfoTabJsonObject.put("totalItemCount", 0);
                    InfoTabJsonObject.put("label", "Info");
                    InfoTabJsonObject.put("name", "info");
                    mProfileTabs.put(1, InfoTabJsonObject);
并将此JsonObject添加到索引1处的JsonArray中,但在添加此JsonObject后,它将用新的JsonObject替换原来的第一个索引的JsonObject

我如何才能实现指数应该保持正确,这意味着新的一个将被添加到第一个指数,另一个将调整他们的指数


请在此帮助我,谢谢您的帮助。

请查看下面的代码,使用
org.json.simple.JSONArray
org.json.simple.JSONObject

    JSONObject jsonObj1 = new JSONObject();
    jsonObj1.put("totalItemCount", 0);
    jsonObj1.put("label", "Updates");
    jsonObj1.put("name", "update");

    JSONObject jsonObj2 = new JSONObject();
    jsonObj2.put("totalItemCount", 2);
    jsonObj2.put("label", "Members");
    jsonObj2.put("url", "groups/member/list/129");
    JSONArray temp = new JSONArray();
    jsonObj2.put("urlParams", temp);
    jsonObj2.put("name", "members");

    JSONObject jsonObj3 = new JSONObject();
    jsonObj3.put("totalItemCount", 5);
    jsonObj3.put("label", "Photos");
    jsonObj3.put("url", "groups/photo/list");
    JSONObject temp1 = new JSONObject();
    temp1.put("group_id", 129);
    jsonObj3.put("urlParams", temp1);
    jsonObj3.put("name", "photos");

    JSONArray array=new JSONArray();
    array.add(jsonObj1); //adding objects into array 
    array.add(jsonObj2);
    array.add(jsonObj3);
    try {  

        // Writing to a file  
        System.out.println("Before addition of new object into array!");
        File file=new File("test1.json");  
        file.createNewFile();  
        FileWriter fileWriter = new FileWriter(file);  
        fileWriter.write(array.toJSONString());  
        fileWriter.flush();  
        fileWriter.close();  

    } catch (IOException e) {  
        e.printStackTrace();  
    }

    JSONObject InfoTabJsonObject = new JSONObject();
    InfoTabJsonObject.put("totalItemCount", 0); //adding objects into array
    InfoTabJsonObject.put("label", "Info");
    InfoTabJsonObject.put("name", "info");
    array.add(1, InfoTabJsonObject);
    System.out.println("After addition of new object into array!");
    try {  

        // Writing to a file  
        File file=new File("test.json");  
        file.createNewFile();  
        FileWriter fileWriter = new FileWriter(file);  
        fileWriter.write(array.toJSONString());  
        fileWriter.flush();  
        fileWriter.close();  

    } catch (IOException e) {  
        e.printStackTrace();  
    } 
test1.json是在数组中添加新对象之前的原始值

[
{
    "name": "update",
    "label": "Updates",
    "totalItemCount": 0
},
{
    "urlParams": [],
    "name": "members",
    "label": "Members",
    "url": "groups\/member\/list\/129",
    "totalItemCount": 2
},
{
    "urlParams": {
        "group_id": 129
    },
    "name": "photos",
    "label": "Photos",
    "url": "groups\/photo\/list",
    "totalItemCount": 5
}
]
[
{
    "name": "update",
    "label": "Updates",
    "totalItemCount": 0
},
{
    "name": "info",
    "label": "Info",
    "totalItemCount": 0
},
{
    "urlParams": [],
    "name": "members",
    "label": "Members",
    "url": "groups\/member\/list\/129",
    "totalItemCount": 2
},
{
    "urlParams": {
        "group_id": 129
    },
    "name": "photos",
    "label": "Photos",
    "url": "groups\/photo\/list",
    "totalItemCount": 5
}
]
test.json在数组中添加新对象后更新的一个

[
{
    "name": "update",
    "label": "Updates",
    "totalItemCount": 0
},
{
    "urlParams": [],
    "name": "members",
    "label": "Members",
    "url": "groups\/member\/list\/129",
    "totalItemCount": 2
},
{
    "urlParams": {
        "group_id": 129
    },
    "name": "photos",
    "label": "Photos",
    "url": "groups\/photo\/list",
    "totalItemCount": 5
}
]
[
{
    "name": "update",
    "label": "Updates",
    "totalItemCount": 0
},
{
    "name": "info",
    "label": "Info",
    "totalItemCount": 0
},
{
    "urlParams": [],
    "name": "members",
    "label": "Members",
    "url": "groups\/member\/list\/129",
    "totalItemCount": 2
},
{
    "urlParams": {
        "group_id": 129
    },
    "name": "photos",
    "label": "Photos",
    "url": "groups\/photo\/list",
    "totalItemCount": 5
}
]

你找到解决办法了吗?我刚刚遇到了同样的问题,我正要问一个问题,但我发现了你的问题。