給定 JSON 物件
{
"property": "value"
}
如果您對這個物件執行 JSON 補丁,如下所示
[{
"op": "add",
"path": "/otherProperty/property",
"value": "childvalue"
}]
JSON 補丁操作是否應該因為otherProperty未定義而失敗,還是應該添加整個路徑?
我在這方面找不到任何資訊。
uj5u.com熱心網友回復:
正如評論中提到的,JSON Patch Internet Draft 指出操作應該會導致錯誤:
However, the object itself or an array containing it does need to
exist, and it remains an error for that not to be the case. For
example, an "add" with a target location of "/a/b" starting with this
document:
{ "a": { "foo": 1 } }
is not an error, because "a" exists, and "b" will be added to its
value. It is an error in this document:
{ "q": { "bar": 2 } }
because "a" does not exist.
也就是說,您仍然可以做您想做的事,但是您必須通過添加一個包含您想要的屬性的物件來更改語法。因此,根據該草案的附錄 10,您可以執行以下操作
[{
"op": "add",
"path": "/otherProperty",
"value": { "property" : "childvalue" }
}]
在這種情況下,您將在根級別創建一個欄位,該欄位將 json 物件作為主體:
{
"property": "value",
"otherProperty" : {
"property" : "childvalue"
}
}
我在這里通過在目標資源的 JSON 前后粘貼來測驗它,它生成了與我上面介紹的相同的 add 陳述句。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/390002.html
