英文:
How to remove json key value pair from json object array using java
问题
我想从存储在jsonObject中的JSON中删除"status": "new",使用以下代码:
JSONParser parser = new JSONParser();
Object obj = parser.parse(responseStr);
jsonObject = (JSONObject) obj;
JSON结构 --
{"actionName": "test"
"Data": [{
"isActive": true,
"Id": "1358",
"status": "new"
}],
}
新的JSON应该是这样的 --
{"actionName": "test"
"Data": [{
"isActive": true,
"Id": "1358"
}],
}
我尝试过jsonObj.remove("status"),但没有成功。
英文:
I want to remove "status": "new" from JSON that I have stored in jsonObject using
JSONParser parser = new JSONParser();
Object obj = parser.parse(responseStr);
jsonObject = (JSONObject) obj;
JSON structure --
{"actionName": "test"
"Data": [{
"isActive": true,
"Id": "1358",
"status": "new"
}],
}
new JSON should look like this -
{"actionName": "test"
"Data": [{
"isActive": true,
"Id": "1358"
}],
}
I have tried jsonObj.remove("status") , but no luck.
答案1
得分: 1
jsonObj.getJSONArray("Data").get(0).remove("status");
#Updated Code-breakdown:
JSONObject obj = (JSONObject) jsonObj.getJSONArray("Data").get(0);
obj.remove("status");
JSONArray newArr = new JSONArray();
newArr.put(obj);
jsonObj.put("Data", newArr);
It should do your work, haven't tested though.
First, your 'Data' is JSONArray, retrieving that by jsonObj.getJSONArray("Data"), then access the array with get(0)[assuming, your array will contain only one entry like your example] and finally, removing that key by remove method.
英文:
jsonObj.getJSONArray("Data").get(0).remove("status);
#Updated Code-breakdown:
JSONObject obj= (JSONObject) jsonObj.getJSONArray("Data").get(0);
obj.remove("status");
JSONArray newArr=new JSONArray();
newArr.put(obj);
jsonObj.put("Data", newArr);
It should do your work, haven't tested though.
First, your Data
is JSONArray, retrieving that by jsonObj.getJSONArray("Data")
, then access the array with get(0)
[assuming, your array will contain only one entry like your example] and finally, removing that key by remove
method.
答案2
得分: 0
你需要迭代 Data
属性,该属性是一个 JSON 数组
,并且需要删除每个项目的 status
键。
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.File;
import java.io.FileReader;
public class JsonSimpleApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
JSONParser parser = new JSONParser();
JSONObject root = (JSONObject) parser.parse(new FileReader(jsonFile));
JSONArray dataArray = (JSONArray) root.get("Data");
dataArray.forEach(item -> {
JSONObject object = (JSONObject) item;
object.remove("status");
});
System.out.println(root);
}
}
上述代码输出:
{"Data":[{"Id":"1358","isActive":true}],"actionName":"test"}
英文:
You need to iterate over Data
property which is a JSON Array
and remove for every item status
key.
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.File;
import java.io.FileReader;
public class JsonSimpleApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
JSONParser parser = new JSONParser();
JSONObject root = (JSONObject) parser.parse(new FileReader(jsonFile));
JSONArray dataArray = (JSONArray) root.get("Data");
dataArray.forEach(item -> {
JSONObject object = (JSONObject) item;
object.remove("status");
});
System.out.println(root);
}
}
Above code prints:
{"Data":[{"Id":"1358","isActive":true}],"actionName":"test"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论