ad

在特定目录中将键放入JSON中,将键放入所有目录中(jsonpath.parse(string).put)-英雄云拓展知识分享

匿名投稿 301 2024-01-22

我有一个看起来像这样的JSON模板。

         {

"parent": {

"dir1": {

"key11": {

"min": 1,

"max": 100

},

在特定目录中将键放入JSON中,将键放入所有目录中(jsonpath.parse(string).put)-英雄云拓展知识分享

"key12": {

"sample1": "txt",

"sample2": "100.25"

}

},

"dir2": {

"key21": {

"min": 1,

"max": 100

},

"key22": {

"sample1": "USD",

"sample2": "100.25"

}

}

}

}

在此,我有一个逻辑来解析JSON并检查(迭代时),如果parent.dir1有一些特定的键,例如“ Goldenkey”。如果找不到键,则将()放在jsonpath jsonpath.parse.parse(jsonstring)中的“键(goldenkey)” .put(jsonpath,key,value).json();

为了获得jsonpath放置钥匙,我将经过“将当前的jsonpath”拆分为“”。从最后一个转换为需要放置的密钥。以下代码

try {

//set jsonpath, if key not found then catch

JsonPath.parse(json).set("$.parent.dir1.goldenKey", "golden").json();

} catch (final PathNotFoundException e) {

//json path of the key

String jsonpath = "$.parent.dir1.goldenKey";

//get the key by getting last index of "."

final String key = jsonpath.substring(jsonpath.lastIndexOf(".") +1);

jsonpath = jsonpath.substring(0, jsonpath.lastIndexOf("."));

JsonPath.parse(json).put(jsonpath, "goldenKey", "golden").json();}

问题是,在给定的JSON路径(即此代码中的DIR1)上,它不用放置密钥,而是将键和值放在DIR1和DIR23中

请让我知道解释是不是不清楚。

看答案

您正在从硬编码的JSONPATH中获得子字符串。 jsonpath应当是 $.parent.dir1 添加值时 "goldenKey":"golden"。这应当仅在DIR1下添加钥匙值对。仔细检查一次。


🚀🌟 点击注册 免费试用超级应用平台-英雄云企业级hpapaas 🌟🚀 😃👉🌐

免责声明:

本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。

标签:爪哇 JSON
上一篇:VBA宏找不到ActiveCell的最后一个使用的单元格-英雄云拓展知识分享
下一篇:Mozilla停止父母锚标签行动-英雄云拓展知识分享
相关文章

 发表评论

暂时没有评论,来抢沙发吧~

×