如何在Java中从JSON字符串中删除元素?
我有一个json作为字符串,我需要使用java代码从中删除一个元素。谢谢你的帮助 例如 尝试了数组和其他东西,但没有运气 输入:需要删除图像如何在Java中从JSON字符串中删除元素?,java,json,Java,Json,我有一个json作为字符串,我需要使用java代码从中删除一个元素。谢谢你的帮助 例如 尝试了数组和其他东西,但没有运气 输入:需要删除图像 {"widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 },
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
输出:
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
安装并导入此软件包:
import org.json.*;
并使用以下代码:
try {
String src = "{\"widget\": {\n"
+ " \"debug\": \"on\",\n"
+ " \"window\": {\n"
+ " \"title\": \"Sample Konfabulator Widget\",\n"
+ " \"name\": \"main_window\",\n"
+ " \"width\": 500,\n"
+ " \"height\": 500\n"
+ " },\n"
+ " \"image\": { \n"
+ " \"src\": \"Images/Sun.png\",\n"
+ " \"name\": \"sun1\",\n"
+ " \"hOffset\": 250,\n"
+ " \"vOffset\": 250,\n"
+ " \"alignment\": \"center\"\n"
+ " },\n"
+ " \"text\": {\n"
+ " \"data\": \"Click Here\",\n"
+ " \"size\": 36,\n"
+ " \"style\": \"bold\",\n"
+ " \"name\": \"text1\",\n"
+ " \"hOffset\": 250,\n"
+ " \"vOffset\": 100,\n"
+ " \"alignment\": \"center\",\n"
+ " \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"\n"
+ " }\n"
+ "}} ";
JSONObject obj = new JSONObject(src);
obj.getJSONObject("widget").remove("image");
System.out.println("obj: " + obj);
} catch (JSONException ex) {
ex.printStackTrace();
}
希望这对你有帮助 下面是使用不同流行库的示例代码,以实现以下目标: 杰克逊
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readValue(jsonStr, JsonNode.class);
((ObjectNode) node.get("widget")).remove("image");
System.out.println(node.toString());
Gson gson = new Gson();
JsonElement jsonObj= gson.fromJson(jsonStr, JsonElement.class);
jsonObj.getAsJsonObject().get("widget").getAsJsonObject().remove("image");
System.out.println(jsonObj.toString());
JSONObject jsonObj = new JSONObject(jsonStr);
jsonObj.getJSONObject("widget").remove("image");
System.out.println(jsonObj.toString());
Gson
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readValue(jsonStr, JsonNode.class);
((ObjectNode) node.get("widget")).remove("image");
System.out.println(node.toString());
Gson gson = new Gson();
JsonElement jsonObj= gson.fromJson(jsonStr, JsonElement.class);
jsonObj.getAsJsonObject().get("widget").getAsJsonObject().remove("image");
System.out.println(jsonObj.toString());
JSONObject jsonObj = new JSONObject(jsonStr);
jsonObj.getJSONObject("widget").remove("image");
System.out.println(jsonObj.toString());
抛弃
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readValue(jsonStr, JsonNode.class);
((ObjectNode) node.get("widget")).remove("image");
System.out.println(node.toString());
Gson gson = new Gson();
JsonElement jsonObj= gson.fromJson(jsonStr, JsonElement.class);
jsonObj.getAsJsonObject().get("widget").getAsJsonObject().remove("image");
System.out.println(jsonObj.toString());
JSONObject jsonObj = new JSONObject(jsonStr);
jsonObj.getJSONObject("widget").remove("image");
System.out.println(jsonObj.toString());
使用JSON解析器,然后解析JSON文本,根据需要修改数据,重新格式化为JSON文本。使用Jackson或Gson在java中处理JSON。不要试图修改字符串本身。它是不可维护的。