使用 Newtonsoft 在 C# 中使用 JSON 架构验证 JSON
Validating JSON with JSON Schema in C# using Newtonsoft
使用 JSON 架构 return 验证 JSON 始终为真。
Newtonsoft 用于验证并使用模式和数据测试 here。
它 return 总是 'No errors found. JSON validates against the schema'.
请找到我的 JSON 架构。
{
"schema": {
"definitions": {
},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"widget": { "formlyConfig": { "type": "accordion" } },
"title": "The Root Schema",
"required": [
"accordion1",
"accordion2",
"accordion3"
],
"properties": {
"accordion1": {
"$id": "#/properties/accordion1",
"type": "object",
"title": "The Accordion1 Schema",
"required": [
"firstname",
"age"
],
"properties": {
"firstname": {
"$id": "#/properties/accordion1/properties/firstname",
"type": "string",
"title": "The Firstname Schema",
"default": "firstname pvr1"
},
"age": {
"$id": "#/properties/accordion1/properties/age",
"type": "integer",
"title": "The Age Schema",
"default": 21
}
}
},
"accordion2": {
"$id": "#/properties/accordion2",
"type": "object",
"title": "The Accordion2 Schema",
"required": [
"firstname",
"age"
],
"properties": {
"firstname": {
"$id": "#/properties/accordion2/properties/firstname",
"type": "string",
"title": "The Firstname Schema",
"default": "firstName2"
},
"age": {
"$id": "#/properties/accordion2/properties/age",
"type": "integer",
"title": "The Age Schema",
"default": 31
}
}
},
"accordion3": {
"$id": "#/properties/accordion3",
"type": "object",
"title": "The Accordion3 Schema",
"required": [
"firstname",
"age"
],
"properties": {
"firstname": {
"$id": "#/properties/accordion3/properties/firstname",
"type": "string",
"title": "The Firstname Schema",
"default": "firstnaem3"
},
"age": {
"$id": "#/properties/accordion3/properties/age",
"type": "integer",
"title": "The Age Schema",
"default": 10
}
}
}
},
'additionalProperties': false
}
}
请查找JSON
{
"accordion1":{
"firstname":"JSON ACCORD PALANIVELRAJAN",
"age":29
},
"accordion2":{
"firstname":"JSON ACCORD LAKSHMANAN",
"age":39
},
"accordion3":{
"firstname":null,
"age":49
}
}
我试图将名字更改为整数并删除 accordion1 中的第一个。 return 在所有情况下都是如此。
请指教
请找到使用 JSON 架构验证 JSON 的代码。
模型是一个 JObject,它是一个有效的 JSON。
JsonSchema json_schema = JsonSchema.Parse(schema);
IList<string> messages;
bool valid = model.IsValid(json_schema, out messages);
return valid;
JsonSchema
已弃用,已移至单独的包:Newtonsoft.Json.Schema。使用这个包,我能够根据你的模式验证你的 JSON (我确实删除了外部 schema
元素,因为它实际上是无效的,并导致模式无法正确验证 - 我想你可能'我已经把它放在那里了,因为旧的 JsonSchema
class 否则无法解析模式!),如果我将 JSON 更改为无效的形状,删除了必需的元素,或者将数据更改为无效类型:
string data = File.ReadAllText("data.json");
string schema = File.ReadAllText("data.schema.json");
var model = JObject.Parse(data);
var json_schema = JSchema.Parse(schema);
IList<string> messages;
bool valid = model.IsValid(json_schema, out messages); // properly validates
我正在使用 .NET Core 2.2、Newtonsoft.Json 12.0.2 和 Newtonsoft.Json.Schema 3.0.11,以防万一。请注意,Newtonsoft.Json.Schema 包对商业用途有限制 - 检查许可!
使用 JSON 架构 return 验证 JSON 始终为真。 Newtonsoft 用于验证并使用模式和数据测试 here。 它 return 总是 'No errors found. JSON validates against the schema'.
请找到我的 JSON 架构。
{
"schema": {
"definitions": {
},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"widget": { "formlyConfig": { "type": "accordion" } },
"title": "The Root Schema",
"required": [
"accordion1",
"accordion2",
"accordion3"
],
"properties": {
"accordion1": {
"$id": "#/properties/accordion1",
"type": "object",
"title": "The Accordion1 Schema",
"required": [
"firstname",
"age"
],
"properties": {
"firstname": {
"$id": "#/properties/accordion1/properties/firstname",
"type": "string",
"title": "The Firstname Schema",
"default": "firstname pvr1"
},
"age": {
"$id": "#/properties/accordion1/properties/age",
"type": "integer",
"title": "The Age Schema",
"default": 21
}
}
},
"accordion2": {
"$id": "#/properties/accordion2",
"type": "object",
"title": "The Accordion2 Schema",
"required": [
"firstname",
"age"
],
"properties": {
"firstname": {
"$id": "#/properties/accordion2/properties/firstname",
"type": "string",
"title": "The Firstname Schema",
"default": "firstName2"
},
"age": {
"$id": "#/properties/accordion2/properties/age",
"type": "integer",
"title": "The Age Schema",
"default": 31
}
}
},
"accordion3": {
"$id": "#/properties/accordion3",
"type": "object",
"title": "The Accordion3 Schema",
"required": [
"firstname",
"age"
],
"properties": {
"firstname": {
"$id": "#/properties/accordion3/properties/firstname",
"type": "string",
"title": "The Firstname Schema",
"default": "firstnaem3"
},
"age": {
"$id": "#/properties/accordion3/properties/age",
"type": "integer",
"title": "The Age Schema",
"default": 10
}
}
}
},
'additionalProperties': false
}
}
请查找JSON
{
"accordion1":{
"firstname":"JSON ACCORD PALANIVELRAJAN",
"age":29
},
"accordion2":{
"firstname":"JSON ACCORD LAKSHMANAN",
"age":39
},
"accordion3":{
"firstname":null,
"age":49
}
}
我试图将名字更改为整数并删除 accordion1 中的第一个。 return 在所有情况下都是如此。
请指教
请找到使用 JSON 架构验证 JSON 的代码。
模型是一个 JObject,它是一个有效的 JSON。
JsonSchema json_schema = JsonSchema.Parse(schema);
IList<string> messages;
bool valid = model.IsValid(json_schema, out messages);
return valid;
JsonSchema
已弃用,已移至单独的包:Newtonsoft.Json.Schema。使用这个包,我能够根据你的模式验证你的 JSON (我确实删除了外部 schema
元素,因为它实际上是无效的,并导致模式无法正确验证 - 我想你可能'我已经把它放在那里了,因为旧的 JsonSchema
class 否则无法解析模式!),如果我将 JSON 更改为无效的形状,删除了必需的元素,或者将数据更改为无效类型:
string data = File.ReadAllText("data.json");
string schema = File.ReadAllText("data.schema.json");
var model = JObject.Parse(data);
var json_schema = JSchema.Parse(schema);
IList<string> messages;
bool valid = model.IsValid(json_schema, out messages); // properly validates
我正在使用 .NET Core 2.2、Newtonsoft.Json 12.0.2 和 Newtonsoft.Json.Schema 3.0.11,以防万一。请注意,Newtonsoft.Json.Schema 包对商业用途有限制 - 检查许可!