我有一個有效的 json 模式,如下所示 { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "abcd", "title": "test schema", "description": "............", "type": "object", "properties": { "a": { ........... ........... }, "b": { ......... ........ ......... }, "c": { ........... .......... }, "d": { ........... .......... } }, "anyOf": [ { "type": "object", "$ref": "#/properties/a", "$ref": "#/properties/b" }, { "type": "object", "$ref": "#/properties/c", "$ref": "#/properties/d" } ] }上面的模式存儲在一個文件中,我正在加載它進行解析,如下所示JSchema schema = JSchema.Parse(File.ReadAllText(@"D:\Backups\testschema.json"));所以當我查看模式的輸出時,它如下所示My Json Schema{ "$schema": "http://json-schema.org/draft-07/schema#", "$id": "abcd", "title": "test schema", "description": "............", "type": "object", "properties": { "a": { ........... ........... }, "b": { ......... ........ ......... }, "c": { ........... .......... }, "d": { ........... .......... } },"anyOf": [ { "$ref": "#/properties/b" }, { "$ref": "#/properties/d" } ]}I'm wondering why I'm getting only the last reference under the anyOf propertyOn parsing shouldn't the output be the same as that in the file?Am I missing something?My desired output under anyOf is "anyOf": [ { "type": "object", "$ref": "#/properties/a", "$ref": "#/properties/b" }, { "type": "object", "$ref": "#/properties/c", "$ref": "#/properties/d" } ]關于如何實現我想要的輸出有什么想法嗎?
1 回答

手掌心
TA貢獻1942條經驗 獲得超3個贊
在 Json 中,每個對象只能有一個特定的鍵一次。所以在一個對象中,你只能有一個名稱為 key 的鍵$ref。您在上面發布的 Json 無效;這取決于它的實現——理想情況下它應該拋出一個錯誤,但在這種情況下,看起來第二個正在覆蓋第一個。
請注意,對于 a $ref,其他屬性將被忽略,因此type除了$ref.
我不完全確定,但看起來你想要實現的是說屬性“a”和“b”應該存在,或者屬性“c”和“d”應該存在。
您可以通過將anyOf子句替換為:
"anyOf": [
{
"required": ["a", "b"]
},
{
"required": ["c", "d"]
}
]
- 1 回答
- 0 關注
- 135 瀏覽
添加回答
舉報
0/150
提交
取消