使用Newtonsoft在C#中使用JSON模式验证JSON
使用JSON模式验证JSON,返回值始终为true。 Newtonsoft用于验证,并使用模式和数据进行测试。 它始终返回“未发现错误”。JSON根据模式“”进行验证 请找到我的JSON模式使用Newtonsoft在C#中使用JSON模式验证JSON,c#,json,json.net,jsonschema,C#,Json,Json.net,Jsonschema,使用JSON模式验证JSON,返回值始终为true。 Newtonsoft用于验证,并使用模式和数据进行测试。 它始终返回“未发现错误”。JSON根据模式“”进行验证 请找到我的JSON模式 { "schema": { "definitions": { }, "$schema": "http://json-schema.org/draft-07/schema#", "$id": "http://example.com/root.json", "typ
{
"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
}
}
我尝试将第一个名称更改为integer,并删除accordion1中的第一个名称。它在所有情况下都返回true
请告知
请查找用JSON模式验证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
JsonSchema json_schema = JsonSchema.Parse(schema);
IList<string> messages;
bool valid = model.IsValid(json_schema, out messages);
return valid;
JsonSchema json_schema=JsonSchema.Parse(schema);
IList消息;
bool valid=model.IsValid(json_模式,输出消息);
返回有效;
JsonSchema
已被弃用,并已移动到单独的包:。使用这个包,我能够根据您的模式验证JSON(我确实删除了外部的schema
元素,因为它实际上是无效的,并导致模式无法正确验证-我想您可能已经将它放在那里了,因为旧的JsonSchema
类无法以其他方式解析模式!),如果我将JSON更改为无效形状、删除必需元素或将数据更改为无效类型,则会收到错误消息:
string data=File.ReadAllText(“data.json”);
stringschema=File.ReadAllText(“data.schema.json”);
var model=JObject.Parse(数据);
var json_schema=JSchema.Parse(schema);
IList消息;
bool valid=model.IsValid(json_模式,输出消息);//正确验证
我正在使用.NETCore2.2、Newtonsoft.Json 12.0.2和Newtonsoft.Json.Schema 3.0.11,以防万一。请注意,Newtonsoft.Json.Schema包对商业用途有限制-请检查许可 感谢您指出“schema”元素。让我检查一下新的包并在这里更新它在删除模式后工作,但我可能还有另一个挑战,那就是一些验证。我引用了一个在客户端可用的函数名,但当我在服务器端进行验证时,将如何验证该函数名。@user3497702我不确定我是否遵循-模式验证用于确保JSON(您发送的或接收的内容)是有效的-服务器端和客户端函数名与此有什么关系?我将提出单独的问题。我也有同样的问题,结果是我的JSON文件被缓存在visual studio中,这就是为什么我一直收到true。