Java 如何使用JsonPath向Json添加新节点?
我正在使用JSON,并且面临一些问题 我想在JSON对象中插入/更新路径。如果路径不存在,将创建它,然后插入一个新值。如果退出,它将被一个新值更新 例如,我想添加如下新路径:Java 如何使用JsonPath向Json添加新节点?,java,json,jsonpath,Java,Json,Jsonpath,我正在使用JSON,并且面临一些问题 我想在JSON对象中插入/更新路径。如果路径不存在,将创建它,然后插入一个新值。如果退出,它将被一个新值更新 例如,我想添加如下新路径: val doc = JsonPath.parse(jsonString) doc.add("$.user.name", "John") 但我总是会遇到这个错误,因为路径不存在: 类com.jayway.jsonpath.PathNotFoundException:路径$['user'中缺少属性 因此,如果路径不存在,我想
val doc = JsonPath.parse(jsonString)
doc.add("$.user.name", "John")
但我总是会遇到这个错误,因为路径不存在:
类com.jayway.jsonpath.PathNotFoundException:路径$['user'中缺少属性
因此,如果路径不存在,我想创建一个新路径
这是我的代码,但jsonString
不会更改:
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")
请给我你的建议。非常感谢 您可以按如下方式执行:
JsonPath.parse(jsonString).set(JsonPath.compile("$.user.name"), "John");
我尝试了三个不同的JSON库,它们都支持JsonPath/JsonPointer(Jackson、JsonPath和JSON-p),但没有一个能够在缺少父节点的情况下重建JSON对象层次结构。因此,我提出了自己的解决方案,使用Jackson/JsonPointer向JSON对象添加新值,因为它允许浏览JsonPointer部分
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());
}
}
这将生成并打印以下JSON对象:
{
"root" : {
"array" : [ {
"name" : "John",
"age" : 17
}, null, null, null, 12 ],
"object" : {
"num" : 81,
"str" : "text"
}
},
"descr" : "description"
}
当然,这并不能覆盖所有的角落案例,但适用于大多数案例。希望这对其他人有所帮助。要创建新节点,请尝试在由JsonPath.parse(jsonString)的结果实现的WriteContext接口上放置(路径、键、对象)。可能有用。这只是跳过缺少路径或节点,但我的json没有得到任何更改。我假设开始的json是空的,如下所示{}我仍然得到这个错误类com.jayway.jsonpath.PathNotFoundException:path$['user']中缺少属性您可以尝试以下操作:private static conf=Configuration.defaultConfiguration().addOptions(Option.DEFAULT\u path\u LEAF\u TO\u NULL)。addOptions(Option.SUPPRESS\u EXCEPTIONS);JsonPath.using(conf).parse(…var jsonString=“{}”val conf=Configuration.defaultConfiguration().addOptions(Option.DEFAULT\u PATH\u LEAF\u TO\u NULL).addOptions(Option.SUPPRESS\u EXCEPTIONS)JsonPath.using(conf).parse(jsonString).set(JsonPath.compile($.user.name”),“John”)Log.d(“TAG”,“new json=$jsonString”)我尝试了你的解决方案,但是jsonString没有改变。你能帮忙吗?我正在使用Kotlin语言。谢谢。它似乎不起作用。你能帮忙吗?谢谢you@NumeroUno即使在配置中,这也适用于单个键,但不适用于嵌套路径;即
工作,但
不工作。
{
"root" : {
"array" : [ {
"name" : "John",
"age" : 17
}, null, null, null, 12 ],
"object" : {
"num" : 81,
"str" : "text"
}
},
"descr" : "description"
}