如何使用Java从JSON对象数组中移除JSON键值对。

huangapple go评论76阅读模式
英文:

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"}

huangapple
  • 本文由 发表于 2020年10月6日 13:03:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64219651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定