我使用JayWay JsonPath来操作JSON对象。
我想在JSON数组中插入一个JSON对象:
### before
{
"students": []
}
### after
{
"students": [{"id": 1, "name": "Bob"}]
}
我无法在JsonPath中实现这一点。如果我将对象作为字符串添加,它不会被识别为JSON对象:
var context = JsonPath.parse("{\"students\": []}");
context.add("students", "{\"id\": 1, \"name\": \"Bob\"}");
System.out.println(context.jsonString());
// > {"students":["{\"id\": 1, \"lastName\": \"Bob\"}"]}
如果我尝试解析对象,JsonPath会添加一个空对象:
var context = JsonPath.parse("{\"students\": []}");
context.add("students", JsonPath.parse("{\"id\": 1, \"name\": \"Bob\"}"));
System.out.println(context.jsonString());
// > {"students":[{}]}
如何将JSON对象插入数组中?
1条答案
按热度按时间mwyxok5s1#
使用
.json()
方法:印刷品
{“学生”:[{“id”:1,“name”:“Bob”}]}