如何使用JsonPath向Json添加新节点? [英] How to add new node to Json using JsonPath?

查看:2306
本文介绍了如何使用JsonPath向Json添加新节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON并遇到一些问题.

I'm working with JSON and facing some problems.

我想在JSON对象中插入/更新路径.如果路径不存在,则会创建该路径,然后插入一个新值.如果退出,它将以新值更新

I want to insert/update a path in a JSON object. In the case that the path doesn't exist, it will be created then I insert a new value. In case that it exits, it will be updated by a new value

例如,我要添加新路径,如下所示:

For example, I want to add new path like this:

val doc = JsonPath.parse(jsonString)
doc.add("$.user.name", "John")

但是我总是会收到此错误,因为该路径不存在:

but I always get this error, because the path doesn't exist:

com.jayway.jsonpath.PathNotFoundException类:路径$ ['user']中缺少属性

class com.jayway.jsonpath.PathNotFoundException : Missing property in path $['user']

因此,如果它不存在,我想创建一个新路径.

Therefore I want to create a new path if it does not exist.

这是我的代码,但jsonString不变:

This is my code, but jsonString doesn't change:

var jsonString = "{}" val conf = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL).addOptions(Option.SUPPRESS_EXCEPTIONS)
JsonPath.using(conf).parse(jsonString).set(JsonPath.compile("$.user.name"), "John") 
Log.d("TAG", "new json = $jsonString") 

请给我您的建议.非常感谢!!

Please give me your advice. Thank you very much!!

推荐答案

我尝试了三种不同的JSON库,并支持JsonPath/JsonPointer(Jackson,JsonPath和JSON-P),但它们都无法在父节点丢失的情况.因此,我想出了自己的解决方案,该方法可以使用Jackson/JsonPointer向JSON对象添加新值,因为它可以浏览JsonPointer部件.

I tried three different JSON libraries with support of JsonPath/JsonPointer (Jackson, JsonPath and JSON-P) and none of them is able to reconstruct JSON object hierarchy in case of missing parent nodes. So I came up with my own solution for adding new values to JSON object using Jackson/JsonPointer as it allows to navigate through JsonPointer parts.

private static final ObjectMapper mapper = new ObjectMapper();

public void setJsonPointerValue(ObjectNode node, JsonPointer pointer, JsonNode value) {
    JsonPointer parentPointer = pointer.head();
    JsonNode parentNode = node.at(parentPointer);
    String fieldName = pointer.last().toString().substring(1);

    if (parentNode.isMissingNode() || parentNode.isNull()) {
        parentNode = StringUtils.isNumeric(fieldName) ? mapper.createArrayNode() : mapper.createObjectNode();
        setJsonPointerValue(parentPointer, parentNode); // recursively reconstruct hierarchy
    }

    if (parentNode.isArray()) {
        ArrayNode arrayNode = (ArrayNode) parentNode;
        int index = Integer.valueOf(fieldName);
        // expand array in case index is greater than array size (like JavaScript does)
        for (int i = arrayNode.size(); i <= index; i++) {
            arrayNode.addNull();
        }
        arrayNode.set(index, value);
    } else if (parentNode.isObject()) {
        ((ObjectNode) parentNode).set(fieldName, value);
    } else {
        throw new IllegalArgumentException("`" + fieldName + "` can't be set for parent node `"
                + parentPointer + "` because parent is not a container but " + parentNode.getNodeType().name());
    }
}

用法:

ObjectNode rootNode = mapper.createObjectNode();

setJsonPointerValue(rootNode, JsonPointer.compile("/root/array/0/name"), new TextNode("John"));
setJsonPointerValue(rootNode, JsonPointer.compile("/root/array/0/age"), new IntNode(17));
setJsonPointerValue(rootNode, JsonPointer.compile("/root/array/4"), new IntNode(12));
setJsonPointerValue(rootNode, JsonPointer.compile("/root/object/num"), new IntNode(81));
setJsonPointerValue(rootNode, JsonPointer.compile("/root/object/str"), new TextNode("text"));
setJsonPointerValue(rootNode, JsonPointer.compile("/descr"), new TextNode("description"));

System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode));

这将生成并打印以下JSON对象:

This generates and prints the following JSON object:

{
  "root" : {
    "array" : [ {
      "name" : "John",
      "age" : 17
    }, null, null, null, 12 ],
    "object" : {
      "num" : 81,
      "str" : "text"
    }
  },
  "descr" : "description"
}

可以肯定的是,这并不适用于所有极端情况,但适用于大多数情况.希望这对其他人有帮助.

For sure, this doesn't cover all corner cases but works in most of the cases. Hope this helps someone else.

这篇关于如何使用JsonPath向Json添加新节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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