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

我有一个看起来像的 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"

 }

}

字符串数组是外部化的。它将来也可能具有其他价值。有人可以建议我一个很好的链接或想法来解决这个问题吗?


开心每一天1111
浏览 121回答 1
1回答

UYOU

我能想到的最灵活的方法是使用杰克逊的JsonAnyGetter注释。它允许您向 Jackson 提供Map您的 pojo 状态的表示。从 a 中过滤值Map可以以迭代方式完成。Map从包含s的 a 中过滤值Map可以递归方式完成。这是我根据提供的问题构建的解决方案public class Claim {&nbsp; &nbsp; Map<String, Object> properties = new HashMap<>();&nbsp; &nbsp; public Claim() {&nbsp; &nbsp; &nbsp; &nbsp; // may be populated from instance variables&nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> person = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; person.put("name", "Sam");&nbsp; &nbsp; &nbsp; &nbsp; person.put("surname", "ngonma");&nbsp; &nbsp; &nbsp; &nbsp; properties.put("person", person);&nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> car = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; car.put("make", "Toyota");&nbsp; &nbsp; &nbsp; &nbsp; car.put("model", "yaris");&nbsp; &nbsp; &nbsp; &nbsp; properties.put("car", car);&nbsp; &nbsp; }&nbsp; &nbsp; // nullify map values based on provided array&nbsp; &nbsp; public void filterProperties (String[] nullifyValues) {&nbsp; &nbsp; &nbsp; &nbsp; filterProperties(properties, nullifyValues);&nbsp; &nbsp; }&nbsp; &nbsp; // nullify map values of provided map based on provided array&nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; private void filterProperties (Map<String, Object> properties, String[] nullifyValues) {&nbsp; &nbsp; &nbsp; &nbsp; // iterate all String-typed values&nbsp; &nbsp; &nbsp; &nbsp; // if value found in array arg, nullify it&nbsp; &nbsp; &nbsp; &nbsp; // (we iterate on keys so that we can put a new value)&nbsp; &nbsp; &nbsp; &nbsp; properties.keySet().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(key -> properties.get(key) instanceof String)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(key -> Arrays.asList(nullifyValues).contains(properties.get(key)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(key -> properties.put(key, null));&nbsp; &nbsp; &nbsp; &nbsp; // iterate all Map-typed values&nbsp; &nbsp; &nbsp; &nbsp; // call this method on value&nbsp; &nbsp; &nbsp; &nbsp; properties.values().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(value -> value instanceof Map)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(value -> filterProperties((Map<String, Object>)value, nullifyValues));&nbsp; &nbsp; }&nbsp; &nbsp; // provide jackson with Map of all properties&nbsp; &nbsp; @JsonAnyGetter&nbsp; &nbsp; public Map<String, Object> getProperties() {&nbsp; &nbsp; &nbsp; &nbsp; return properties;&nbsp; &nbsp; }}测试方法public static void main(String[] args) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; Claim claim = new Claim();&nbsp; &nbsp; &nbsp; &nbsp; claim.filterProperties(new String[]{"Sam", "Toyota"});&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(claim));&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}输出{"car":{"model":"yaris","make":null},"person":{"surname":"ngonma","name":null}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java