我有一个类似于以下内容的 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/