Java 从 JsonObject 内的 Json 中删除键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34092346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:18:16  来源:igfitidea点击:

Remove key from a Json inside a JsonObject

javajson

提问by Anoop Philip

I have JsonObject like below

我有像下面这样的 JsonObject

{"status":"ACTIVE","accounts":{"email":"[email protected]","name":"Test"}}

How can I remove Json key "email" and its value from the JsonObject by using something like jsonObj.remove("email")in java

如何使用类似jsonObj.remove("email")java 的东西从 JsonObject 中删除 Json 键“email”及其值

JsonObj.removev working for me if I need to remove status key jsonObj.remove("status")

如果我需要删除状态键,JsonObj.removev 为我工作 jsonObj.remove("status")

Update

更新

Some more background, This is for mainly testing a rest end point. In my test I created java object matching to payload with Builder pattern and then covert to Json using GsonBuilder like

一些更多的背景,这主要是为了测试一个休息终点。在我的测试中,我使用 Builder 模式创建了与有效负载匹配的 java 对象,然后使用 GsonBuilder 转换为 Json,例如

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
public class JsonConvertor() {
public static JsonObject convertToJsonObject(Object payload) {
GsonBuilder builder = new GsonBuilder();
return (JsonObject) builder.setFieldNamingPolicy(FieldNamingPolicy.Policy).
            create().toJsonTree(payload);
}}

If I need to remove a required field I use, JsonObj.remove("key") on Json Object created by above function.

如果我需要删除我使用的必填字段, JsonObj.remove("key") 在由上述函数创建的 Json 对象上。

采纳答案by Thomas

If Gson is like Hymanson (I assume so) you'll have to first get the JsonObject"accounts" from the root object and then remove the member "email", e.g. like this:

如果 Gson 像 Hymanson (我认为是这样),您必须首先JsonObject从根对象获取“帐户”,然后删除成员“电子邮件”,例如像这样:

jsonObj.getAsJsonObject("accounts").remove("email");

Alternatively - and probably the preferred way - you would map the json object to a POJO (one that has the fields "status", "accounts" and "accounts" would point to another POJO), navigate to the accounts-POJO and set "email" to null there. Then you reformat the root POJO to JSON and apply a setting that omits fields with null values.

或者 - 可能是首选方式 - 您可以将 json 对象映射到一个 POJO(具有“status”、“accounts”和“accounts”字段的将指向另一个 POJO),导航到 accounts-POJO 并设置“电子邮件”在那里为空。然后,您将根 POJO 重新格式化为 JSON 并应用一个省略具有空值的字段的设置。

Edit(answer to the question in the comment):

编辑(回答评论中的问题):

To make it short, I don't know whether there is a built-in functionality or not but it should be doable.

简而言之,我不知道是否有内置功能,但它应该是可行的。

The problem is that if you just provide keys like emailetc. you might get situations where there are multiple keys so identifying the correct one could be hard. Thus it might be better to provide the key as accounts.emailand split the "key" into sub-expressions and then traverse the Json tree using the parts or convert the Json to a POJO and use some expression language (e.g. Java EL or OGNL) to traverse the POJO.

问题是,如果您只提供诸如email等之类的键,您可能会遇到有多个键的情况,因此识别正确的键可能很困难。因此,最好提供密钥 asaccounts.email并将“密钥”拆分为子表达式,然后使用部分遍历 Json 树或将 Json 转换为 POJO 并使用某种表达式语言(例如 Java EL 或 OGNL)来遍历POJO。

If you want to remove all properties named emailyou could just travers the entire json tree, check whether there is a property with that name and if so remove it.

如果要删除所有命名的属性email,只需遍历整个 json 树,请检查是否存在具有该名称的属性,如果有,则将其删除。

回答by nvntkmr

Alternatively, you can use below:

或者,您可以在下面使用:

DocumentContext doc = JsonPath.parse(json);
doc.delete(jsonPath);

Where jsonand and jsonPathare strings.

wherejson和 andjsonPath是字符串。

Library: com.jayway.jsonpath.DocumentContext.

库:com.jayway.jsonpath.DocumentContext