我正在使用如下的JsonAarray:
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。
现在我想在索引1的JsonArray中添加一个新的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。
如何才能使索引保持正常,意味着新索引将被添加到第一个索引而另一个将调整其索引。
请在这里帮助我,谢谢你。
答案 0 :(得分:0)
请查看以下代码,使用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
}
]
test.json 在数组中添加新对象后更新的。
[
{
"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
}
]