从JsonObject中的Json中删除键 [英] Remove key from a Json inside a JsonObject

查看:1894
本文介绍了从JsonObject中的Json中删除键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JsonObject,如下所示

I have JsonObject like below

{"status":"ACTIVE","accounts":{"email":"email@gmail.com","name":"Test"}}

如何删除通过在java中使用类似 jsonObj.remove(email)的东西,Json键email及其来自JsonObject的值

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

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

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

更新

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

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

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);
}}

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

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

推荐答案

如果Gson像Jackson一样(我假设是这样)你必须先得到 JsonObject 来自根对象的帐户,然后删除成员email,例如像这样:

If Gson is like Jackson (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:

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

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

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.

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

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.

问题在于,如果您只提供电子邮件等密钥,您可能会遇到有多个密钥的情况,因此确定正确的密钥可能是硬。因此,最好将密钥作为 accounts.email 提供,并将key拆分为子表达式,然后使用部件遍历Json树或将Json转换为POJO并使用一些表达式语言(例如Java EL或OGNL)遍历POJO。

The problem is that if you just provide keys like email etc. 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.email and 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 <的所有属性/ code>你可以遍历整个json树,检查是否有一个具有该名称的属性,如果是,则删除它。

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

这篇关于从JsonObject中的Json中删除键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆