我有一个像这样的 json 对象:
{
"content" : [
{
"id" : 54
"foo" : "bar"
},
{
"id" : 43
"foo" : "bar"
},
{
"id" : 76
"foo" : "bar"
}
]
}
如果我想向内容数组添加多个项目(顺序无关紧要),我可以使用带有单行/操作的 json 补丁添加到它吗?
{ "op": "add", "path": "/content/-", "value": [
{
"id" : 34
"foo" : "bar"
},
{
"id" : 23
"foo" : "bar"
},
{
"id" : 87
"foo" : "bar"
}
]
}
或者我是否必须为每个要添加的对象添加一行?
编辑:要清楚我想追加,而不是替换内容。
最佳答案
不幸的是,这似乎(还?)不可能......正如 Json Patch 规范所述:
https://tools.ietf.org/html/rfc6902#section-4.1
The "add" operation performs one of the following functions, depending upon what the target location references:
If the target location specifies an array index, a new value is inserted into the array at the specified index.
If the target location specifies an object member that does not already exist, a new member is added to the object.
If the target location specifies an object member that does exist, that member's value is replaced.
关于arrays - 在单个 JSON Patch 操作中向数组添加多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59849760/