亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果 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 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號