【问题标题】:remove json object specfic values删除 json 对象特定的值
【发布时间】:2022-10-13 10:00:50
【问题描述】:

JSON 表示 水果: { “类型”:“甜”, “价值”:“苹果” } 我想执行这样的简洁表示。

水果:“苹果”

【问题讨论】:

  • 您使用的是 jackson 还是 gson 库?
  • 杰克逊图书馆

标签: spring boot


【解决方案1】:
const data = '{  "fruit": {    "type": "sweet",    "value": "Apple"  }}';
const obj = JSON.parse(data);

for (let key in obj) {
    console.log(key, obj[key].value); // "fruit",  "Apple" 
}

【讨论】:

  • 谢谢,你能指导我得到像这种水果这样的邮递员的回复吗:{“type”:“sweet”,“value”:“Apple”},我同样认为我可以如何使用 Spring Boot 应用程序
  • 通过添加有关代码的作用以及它如何帮助 OP 的更多信息,可以改进您的答案。
【解决方案2】:

您需要将字符串解析为 JsonNode,然后遍历节点并替换其值,同时检查非空子节点以避免将单个节点替换为空值。

public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
    String data =
            "{ "id": "123", "type": "fruit", "veritey1": { "type": "Property", "value": "moresweetappler" }, "quantity": { "type": "Property", "value":10 } }";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode nodes = mapper.readTree(data);
    Iterator<Entry<String, JsonNode>> iterator = nodes.fields();
    while (iterator.hasNext()) {
        Entry<String, JsonNode> node = iterator.next();
        if (node.getValue().hasNonNull("value")) {
            ((ObjectNode) nodes).set(node.getKey(), node.getValue().get("value"));
        }
    }
    System.out.println(nodes.toString());
}

输出: {"id":"123","type":"fruit","veritey1":"moresweetappler","quantity":10}

public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
    String data =
            "{ "tank": { "type": "Relationship", "object": "id007", "time": "2017-07-29T12:00:04Z", "providedBy": { "type": "Relationship", "object": "id009" } } }";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode nodes = mapper.readTree(data);
    Iterator<Entry<String, JsonNode>> iterator = nodes.fields();
    while (iterator.hasNext()) {
        Entry<String, JsonNode> node = iterator.next();
        reduceJson(node.getValue());
    }
    System.out.println(nodes.toString());
}

public static void reduceJson(JsonNode node) {
    if (node.hasNonNull("type")) {
        ((ObjectNode) node).remove("type");
    }
    Iterator<Entry<String, JsonNode>> iterator = node.fields();
    while (iterator.hasNext()) {
        Entry<String, JsonNode> childnode = iterator.next();
        if (childnode.getValue().isObject()) {
            reduceJson(node.get(childnode.getKey()));
        }
    }
}

输出: {"tank":{"object":"id007","time":"2017-07-29T12:00:04Z","providedBy":{"object":"id009"}}}

【讨论】:

  • 谢谢您的答复。但假设我们有这样的实体 { "id": "123", "type": "fruit", "veritey1": { "type": "Property", "value": "moresweetappler" }, "quantity" : { "type": "Property", "value":10 } } 然后上面 sn-p 做所有剩余的值是 null 所以请只为 "type":"Property" 提供一个 Null 值
  • 请提供{ "id": "123", "type": "fruit", "veritey1": { "type": "Property", "value": "moresweetappler" }, "quantity": { "type": "Property", "value":10 } }的预期输出
  • 预期的输出是这个{“id”:“123”,“type”:“fruit”,“veritey1”:moresweetappler”,“quantity”:10}
  • 更新了答案。请检查。
  • 感谢您的宝贵回复,在另一种情况下,我在这个实例上有另一个 JSON 实例,我想对这个 arttr 执行优化。“坦克”:{“类型”:“关系”,“对象”:“id007”,“时间”:“2017-07-29T12:00:04Z”,“providedBy”:{“类型”:“关系”, “对象”:“id009”}}我希望这个 JSON 表示喜欢这个“坦克”:{“对象”:“id007”,“时间”:“2017-07-29T12:00:04Z”,“providedBy”:{“对象”:“id009”}}请指导我如何在同一实体“id”上执行此操作:123
猜你喜欢
  • 2022-10-16
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 2015-07-18
相关资源
最近更新 更多