如何使用Java删除JSONArray元素

yar*_*997 5 java json hibernate extjs4

我的JsonArray是

[{
"Id": null,
"Name": "One New task",
"StartDate": "2010-02-03T05:30:00",
"EndDate": "2010-02-04T05:30:00",
"Duration": 1,
"DurationUnit": "d",
"PercentDone": 0,
"ManuallyScheduled": false,
"Priority": 1,
"parentId": 8,
"index": 0,
"depth": 3,
"checked": null },{
"Id": null,
"Name": "New task",
"StartDate": "2010-02-04T05:30:00",
"EndDate": "2010-02-04T05:30:00",
"Duration": 0,
"DurationUnit": "d",
"PercentDone": 0,
"ManuallyScheduled": false,
"Priority": 1,
"parentId": 8,
"index": 1,
"depth": 3,
"checked": null }]
Run Code Online (Sandbox Code Playgroud)

现在从这个JsonArray我想删除Id,ManuallyScheduled,checked,

我尝试使用jsonArray.remove(1),并jsonArray.discard("Id")在JAVA.但没有任何反应.删除数组项目我做错了什么?

我使用JAVA作为我的技术.

jab*_*lab 9

你有什么是一系列的对象.因此,您必须遍历数组并从每个对象中删除必要的数据,例如

for (int i = 0, len = jsonArr.length(); i < len; i++) {
    JSONObject obj = jsonArr.getJSONObject(i);
    // Do your removals
    obj.remove("id");
    // etc.
}
Run Code Online (Sandbox Code Playgroud)

我假设你正在使用org.json.JSONObjectorg.json.JSONArray这里,但主要仍然是相同的你使用任何JSON处理库.

如果你想转换类似的东西[{"id":215},{"id":216}],[215,216]你可以这样:

JSONArray intArr = new JSONArray();
for (int i = 0, len = objArr.length(); i < len; i++) {
    intArr.put(objArr.getJSONObject(i).getInt("id"));
}
Run Code Online (Sandbox Code Playgroud)