gpt4 book ai didi

java - 在文档上下文中添加 arrayJson 时,数组带有空 json

转载 作者:行者123 更新时间:2023-12-02 04:31:48 24 4
gpt4 key购买 nike

我有一个类似于以下内容的 json:

JSONObject jsonToReplace =new JSONObject({"country":"India", 
"city":[{"cityName":"city1", "temprature":30},{"cityName":"city2", "temprature":40}]});

现在我有了另一个值:

JSONArray newcity = new JSONArray("[{"cityName":"city3", "temprature":20},{"cityName":"city4", "temprature":20}]");

我正在使用com.jayway.jsonpath.DocumentContext来替换该值。

doc.set("city", newcity);

作为回应,我得到了

{"country":"India", 
"city":[{},{}]}

预计

{"country":"India", 
"city":[{"cityName":"city3", "temprature":20},{"cityName":"city4", "temprature":20}]}

DocumentContext doc = JsonPath.parse(JsonToModify);

doc.set("city", newcity);

最佳答案

问题出在 newcity 的定义上。您在双引号内使用双引号,这尚不清楚。在双引号中使用单引号就可以了。

 JSONArray newcity = new JSONArray("[{'cityName':'city3', 'temprature':20},{'cityName':'city4', 'temprature':20}]");

关于java - 在文档上下文中添加 arrayJson 时,数组带有空 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579274/

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