亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 anyOf 屬性下解析具有多個引用的 json 模式時,輸出中僅返回一個引用(最后一個)

在 anyOf 屬性下解析具有多個引用的 json 模式時,輸出中僅返回一個引用(最后一個)

C#
jeck貓 2022-11-22 16:20:44
我有一個有效的 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"]

    }

]


查看完整回答
反對 回復 2022-11-22
  • 1 回答
  • 0 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號