gpt4 book ai didi

java - 设置嵌套 json 数组的值

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:24 26 4
gpt4 key购买 nike

我有一个嵌套的 JSON 文档,我想在嵌入的 JSON 数组中设置特定值,

例如:-

{
"name":"Jack",
"Address":{
"secondaryAddress":[{"name":"JOHN's Address",
"street":"new ave",
"city":"Canada",
"mobile":123456789
},
{"name":"Selena's Address",
"street":"second ave",
"city":"Canada",
"mobile":987654321
},
{"name":"Jack's Address",
"street":"third ave",
"city":"Canada"
}],
"primaryAddress":{},
}
}

我想在该特定 secondaryAddress 对象中将手机号码从 987654321 更改为 456789123,该对象的手机号码为 987654321。

我在我的项目中使用 jsonpath 来获取/设置值为了得到我会使用:-

$.Address.secondaryAddress[*].mobile

这将返回一组手机号码,例如:-

[
123456789,
987654321
]

对于 set 也类似,

但我不知道如何继续这个场景,我想在数组中设置一个特定的对象。

我尝试使用它,但它仍然设置所有数组对象的值:-
DocumentContext cxt = JsonPath.parse(jsonString);
cxt.set("$.Address.SecondaryAddress[*].mobile",456789123,Criteria.where("$.Address.SecondaryAddress[*].mobile").is("987654321");
请帮忙。

提前致谢:)

最佳答案

$.Address.secondaryAddress.[?(@.mobile=='987654321')]

上面的 jsonpath 查询将为您提供特定的 secondaryAddress 节点,其中 mobile = 987654321使用下面的表达式设置新的移动值-

$.Address.secondaryAddress.[?(@.mobile=='987654321')].mobile

关于java - 设置嵌套 json 数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43540873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com