C# 如何向JSONSchema添加$ref?
我有一个JSONSchema,其中包含一些项目。现在,定义这些项的模式需要在主模式中引用吗C# 如何向JSONSchema添加$ref?,c#,json,json.net,jsonschema,C#,Json,Json.net,Jsonschema,我有一个JSONSchema,其中包含一些项目。现在,定义这些项的模式需要在主模式中引用吗 * one schema that you reference: { "id": "http://some.where/sub/schema#", "type": "object", "properties": { "p1": { "type": "integer", "minimum": 12
* one schema that you reference:
{
"id": "http://some.where/sub/schema#",
"type": "object",
"properties": {
"p1": {
"type": "integer",
"minimum": 12
}
}
}
--- * the main schema: ----
{
"id": "http://path.to/base/schema#",
"type": "array",
"items": {
"extends": {
"$ref": "http://some.where/sub/schema#/properties/p1"
},
"divisibleBy": 5
}
}
另外请注意,我将在项目中有多个项目。我在api中没有看到这样做的方法。api也不允许我添加自定义属性。我怎样才能做到这一点?我正在使用JSON.net 因为评论太长了,我会把它作为一个答案发布。但是你应该根据自己的需要进行定制
string oneSchema = @"{
""id"": ""http://some.where/sub/schema#"",
""type"": ""object"",
""properties"": {
""p1"": {
""type"": ""integer"",
""minimum"": 12
}
}
} ";
string main = @"
{
""id"": ""http://path.to/base/schema#"",
""type"": ""array"",
""items"": {
""extends"": {
""$ref"": ""http://some.where/sub/schema#/properties/p1""
},
""divisibleBy"": 5
}
}";
var JObjMain = (JObject)JsonConvert.DeserializeObject(main);
var jObjOther = (JObject)JsonConvert.DeserializeObject(oneSchema);
JToken src = JObjMain["items"]["extends"]["$ref"];
JToken reference = jObjOther["id"];
var path = src.ToString().Replace(reference.ToString(), "").Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
JToken j = jObjOther[path[0]];
for(int i=1;i<path.Length;i++)
{
j = j[path[i]];
}
src.Replace(j);
Console.WriteLine(JObjMain);
string oneSchema=@{
“id”:http://some.where/sub/schema#"",
“”类型“”:“”对象“”,
“”属性“”:{
“p1”:
“”类型“”:“”整数“”,
“最低限额”:12
}
}
} ";
字符串main=@“
{
“id”:http://path.to/base/schema#"",
“”类型“”:“”数组“”,
“”项目“”:{
“”扩展“”:{
“$ref”:”http://some.where/sub/schema#/properties/p1""
},
“可除数”:5
}
}";
var JObjMain=(JObject)JsonConvert.DeserializeObject(main);
var jObjOther=(JObject)JsonConvert.DeserializeObject(oneSchema);
JToken src=JObjMain[“项目”][“扩展”][“$ref”];
JToken reference=jObjOther[“id”];
var path=src.ToString().Replace(reference.ToString(),“”).Split(新字符[]{'/'},StringSplitOptions.RemoveEmptyEntries);
JToken j=jObjOther[path[0]];
对于(int i=1;i您可以将这两个对象反序列化到JObject
s(使用JsonConvert.DeserializeObject
),并通过一些编码将它们连接起来。您可以发布一个简单的示例吗?因为它有点长,所以我将其作为一个答案发布。