为了账号安全,请及时绑定邮箱和手机立即绑定

如果 JSON 元素中有特定值,则将其更新为 NULL

如果 JSON 元素中有特定值,则将其更新为 NULL

开心每一天1111 2022-07-20 15:50:02
我有一个看起来像的 JSON,{ "person": {  "name":"Sam",   "surname":"ngonma" }, "car": {  "make":"toyota",   "model":"yaris" }}我正在使用以下几行将其写入 Amazon SQS,ObjectMapper mObjectMapper = new ObjectMapper();sqsExtended.sendMessage(new SendMessageRequest(awsSQSUrl, mObjectMapper.writeValueAsString(claim)));我有一个单独的值数组,如果 JSON 在该数组中有它的值,我必须将该字段写为null.如果我的字符串数组是["Sam", "Toyota"]我的最终 JSON 应该是这样的,{ "person": {  "name":null,   "surname":"ngonma" }, "car": {  "make":null,   "model":"yaris" }}字符串数组是外部化的。它将来也可能具有其他价值。有人可以建议我一个很好的链接或想法来解决这个问题吗?
查看完整描述

1 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

我能想到的最灵活的方法是使用杰克逊的JsonAnyGetter注释。它允许您向 Jackson 提供Map您的 pojo 状态的表示。从 a 中过滤值Map可以以迭代方式完成。Map从包含s的 a 中过滤值Map可以递归方式完成。


这是我根据提供的问题构建的解决方案


public class Claim {

    Map<String, Object> properties = new HashMap<>();


    public Claim() {

        // may be populated from instance variables

        Map<String, String> person = new HashMap<>();

        person.put("name", "Sam");

        person.put("surname", "ngonma");

        properties.put("person", person);

        Map<String, String> car = new HashMap<>();

        car.put("make", "Toyota");

        car.put("model", "yaris");

        properties.put("car", car);

    }


    // nullify map values based on provided array

    public void filterProperties (String[] nullifyValues) {

        filterProperties(properties, nullifyValues);

    }


    // nullify map values of provided map based on provided array

    @SuppressWarnings("unchecked")

    private void filterProperties (Map<String, Object> properties, String[] nullifyValues) {

        // iterate all String-typed values

        // if value found in array arg, nullify it

        // (we iterate on keys so that we can put a new value)

        properties.keySet().stream()

            .filter(key -> properties.get(key) instanceof String)

            .filter(key -> Arrays.asList(nullifyValues).contains(properties.get(key)))

            .forEach(key -> properties.put(key, null));

        // iterate all Map-typed values

        // call this method on value

        properties.values().stream()

            .filter(value -> value instanceof Map)

            .forEach(value -> filterProperties((Map<String, Object>)value, nullifyValues));

    }


    // provide jackson with Map of all properties

    @JsonAnyGetter

    public Map<String, Object> getProperties() {

        return properties;

    }

}

测试方法


public static void main(String[] args) {

    try {

        ObjectMapper mapper = new ObjectMapper();

        Claim claim = new Claim();

        claim.filterProperties(new String[]{"Sam", "Toyota"});

        System.out.println(mapper.writeValueAsString(claim));

    } catch (Exception e) {

        e.printStackTrace();

    }

}

输出


{"car":{"model":"yaris","make":null},"person":{"surname":"ngonma","name":null}}


查看完整回答
反对 回复 2022-07-20
  • 1 回答
  • 0 关注
  • 143 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号