验证JSON对JSON模式C# [英] Validate JSON against JSON Schema C#

查看:167
本文介绍了验证JSON对JSON模式C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来验证JSON结构针对该结构的JSON模式?我也看了一下,发现JSON.Net验证,但这并不做我想做的。

Is there a way to validate a JSON structure against a JSON schema for that structure? I have looked and found JSON.Net validate but this does not do what I want.

<一个href=\"http://james.newtonking.com/archive/2009/01/04/json-net-3-5-beta-2-json-schema-validation\">JSON.net作用:

JsonSchema schema = JsonSchema.Parse(@"{
  'type': 'object',
  'properties': {
    'name': {'type':'string'},
    'hobbies': {'type': 'array'}
  }
}");

JObject person = JObject.Parse(@"{
  'name': 'James',
  'hobbies': ['.NET', 'LOLCATS']
}");

bool valid = person.IsValid(schema);
// true

此验证为真。

 JsonSchema schema = JsonSchema.Parse(@"{
  'type': 'object',
  'properties': {
    'name': {'type':'string'},
    'hobbies': {'type': 'array'}
  }
}");

            JObject person = JObject.Parse(@"{
  'surname': 2,
  'hobbies': ['.NET', 'LOLCATS']
}");

            bool valid = person.IsValid(schema);

这也验证为真。

       JsonSchema schema = JsonSchema.Parse(@"{
  'type': 'object',
  'properties': {
    'name': {'type':'string'},
    'hobbies': {'type': 'array'}
  }
}");

            JObject person = JObject.Parse(@"{
  'name': 2,
  'hobbies': ['.NET', 'LOLCATS']
}");

            bool valid = person.IsValid(schema);

只有这个验证为假。

Only this validates to false.

我非常希望它来验证,有又名名称在那里,不应该在那里又名

Ideally I would like it to Validate that there are no fields aka name in there that shouldn't be in there aka surname.

推荐答案

我认为你只需要添加

'additionalProperties': false

你的架构。这将停止正在提供属性未知。

to your schema. This will stop unknown properties being provided.

所以,现在你的结果将是: - 真,假,假

So now your results will be:- True, False, False

测试code ....

test code....

void Main()
{
var schema = JsonSchema.Parse(
@"{
    'type': 'object',
    'properties': {
        'name': {'type':'string'},
        'hobbies': {'type': 'array'}
    },
    'additionalProperties': false
    }");

IsValid(JObject.Parse(
@"{
    'name': 'James',
    'hobbies': ['.NET', 'LOLCATS']
  }"), 
schema).Dump();

IsValid(JObject.Parse(
@"{
    'surname': 2,
    'hobbies': ['.NET', 'LOLCATS']
  }"), 
schema).Dump();

IsValid(JObject.Parse(
@"{
    'name': 2,
    'hobbies': ['.NET', 'LOLCATS']
  }"), 
schema).Dump();
}

public bool IsValid(JObject obj, JsonSchema schema)
{
    return obj.IsValid(schema);
}

输出: -

True
False
False

您还可以添加需要:忠于必须提供这样你可以缺少/无效域的细节返回一个信息字段: -

You could also add "required":true to the fields that must be supplied that way you can return a message with details of missing/invalid fields:-

Property 'surname' has not been defined and the schema does not allow additional     properties. Line 2, position 19. 
Required properties are missing from object: name. 

Invalid type. Expected String but got Integer. Line 2, position 18. 

这篇关于验证JSON对JSON模式C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆