访问 $ref 属性并在 JsonSchema 类中解析其值 JSON.net 更好方法



对于如下所示的 JSON 架构:

JsonSchema propertyJSch = JsonSchema.Parse(
@"{ ""id"" : ""pet"",
   ""properties"" : {
      ""petLicense"" : {
         ""$ref"" : ""#/definitions/petLicense""
      }
   }
}
"
);

假设这将帮助我获得$ref的值:

var petLicValue = propertyJSch.Properties.First().Value;
var petLicValueDict = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(petLicValue.ToString());
var refvalue = petLicValueDict["$ref"];

但这根本行不通。有什么办法可以做到这一点吗?我认为 JSON.Net 库有办法做到这一点,但事实证明它没有。

这是

你应该做的:

  1. 将 json 存储为字符串值

var json = @"{ ""id"" : ""pet"",
                   ""properties"" : {
                      ""petLicense"" : {
                         ""$ref"" : ""#/definitions/petLicense""
                      }
                   }
                }";

  1. 使用动态反序列化字符串

var values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json);

  1. 这是您访问该物业的方式

var r = values["properties"]["petLicense"]["$ref"];

希望这有帮助!

最新更新