首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用JsonPath向Json添加新节点?

如何使用JsonPath向Json添加新节点?
EN

Stack Overflow用户
提问于 2018-08-22 16:49:13
回答 3查看 14.4K关注 0票数 7

我正在使用JSON并面临一些问题。

我想在JSON对象中插入/更新路径。在路径不存在的情况下,它将被创建,然后我插入一个新值。如果它退出,它将被一个新的值更新。

例如,我想添加这样的新路径:

代码语言:javascript
运行
复制
val doc = JsonPath.parse(jsonString)
doc.add("$.user.name", "John")

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

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

因此,如果不存在,我希望创建一条新的路径。

这是我的代码,但是jsonString没有改变:

代码语言:javascript
运行
复制
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") 

请给我你的建议。非常感谢!!

EN

回答 3

Stack Overflow用户

发布于 2018-11-28 23:39:47

我尝试了三个不同的JSON库,支持JsonPath/JsonPointer (Jackson、JsonPath和JSON ),它们都无法在缺少父节点的情况下重建JSON对象层次结构。因此,我想出了自己的解决方案,用于使用Jackson/JsonPointer向JSON对象添加新值,因为它允许在JsonPointer部件中导航。

代码语言:javascript
运行
复制
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());
    }
}

用法:

代码语言:javascript
运行
复制
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对象:

代码语言:javascript
运行
复制
{
  "root" : {
    "array" : [ {
      "name" : "John",
      "age" : 17
    }, null, null, null, 12 ],
    "object" : {
      "num" : 81,
      "str" : "text"
    }
  },
  "descr" : "description"
}

当然,这并不涵盖所有的角落情况,但在大多数情况下都有效。希望这能帮到别人。

票数 12
EN

Stack Overflow用户

发布于 2020-03-20 10:27:43

要在WriteContext接口上创建一个新节点,尝试JsonPath.parse(jsonString)实现的put(路径、键、对象)。

票数 2
EN

Stack Overflow用户

发布于 2018-08-22 16:58:56

你可以这样做:

代码语言:javascript
运行
复制
JsonPath.parse(jsonString).set(JsonPath.compile("$.user.name"), "John");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51971642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档