arrays - 在单个 JSON Patch 操作中向数组添加多个值?

标签 arrays json rest http json-patch

我有一个像这样的 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/

相关文章:

python - 将 Python JSON 库安装到网络服务器上

java.net.URISyntaxException : Illegal character in path at index 75

java - 如何从 RestTemplate postForLocation 获取 STRING 响应?

java - Android Facebook Graph api 基本信息请求

objective-c - 在仍在解析 JSON 时退出 UIViewController 的问题

java - 使用 Play 框架覆盖 HTTP 方法

javascript - 如何在javascript中添加数组的数组

java - 数组从下一个最大值开始遍历

javascript - 原始异常 : Cannot read property 'Value' of undefined

c - 填充时的奇怪行为在 C 中动态分配二维字符数组