c# - Json.NET 根据 Schema 验证 JSON 数组

标签 c# json.net jsonschema

我想验证一个包含数组的模式,所有这些都在对验证方法的一次调用中完成。我是用 javascript 做的,但我正在努力在 C# 中用 Json.NET 做它.使用 Json.NET,我为数组中的每个对象调用验证方法,如下所示:

JSchema schema = JSchema.Parse(@"{
                'title': 'HouseCollection',
    'description': '',
    '$schema': 'http://json-schema.org/draft-04/schema#',
    'definitions': {
                    'Categories': {
                        'title': 'Categories',
            'description': '',
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'type': 'object',
            'additionalProperties': false,
            'properties': {
                            'serviceCode': {
                                'description': 'xxx,
                    'type': 'string'
                            }
                        },
            'required': [
                'serviceCode'
            ]
    },
        'House': {
            'title': 'House',
            'description': '',
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'type': 'object',
            'additionalProperties': false,
            'properties': {
                'aaa': {
                    'type': 'string'
                },
                'bbb': {
                    'type': 'string'
                },
                'ccc': {
                    'description': 'xxx',
                    'type': 'string'
                },
                'ddd': {
                    'type': 'number'
                },
                'eee': {
                    'description': 'xxx',
                    'type': 'boolean'
                },
                'fff': {
                    'description': 'xxx',
                    'type': 'string'
                },
                'ggg': {
                    'description': 'xxx',
                    'type': 'string'
                },
                'hhh': {
                    'type': 'number'
                },
                'iii': {
                    'description': 'xxx',
                    'type': 'string'
                },
                'jjj': {
                    'type': 'string'
                },
                'kkk': {
                    'description': 'xxx',
                    'type': 'string'
                },
                'lll': {
                    'description': 'xxx',
                    'type': 'string'
                },
                'mmm': {
                    'description': '',
                    'type': 'string'
                },
                'nnn': {
                    'description': '',
                    'type': 'array',
                    'items': {
                        '$ref': '#/definitions/Categories'
                    }
                }
            },
            'required': [
                'HouseName'
            ]
        },
        'HouseCollection': {
            '$ref': '#'
        }
    },
    'type': 'object',
    'additionalProperties': false,
    'properties': {
        'houses': {
            'description': '',
            'type': 'array',
            'items': {
                '$ref': '#/definitions/House'
            }
        }
    }
}");

            string housesJsonString = JsonConvert.SerializeObject(houses);
             bool valid = false;
            JArray housesJson = JArray.Parse(housesJsonString);

            foreach (JObject s in housesJson)
            {
                IList<string> messages;
                valid = housesJson.IsValid(schema, out messages);
            }


            return valid;

如何更改此代码以调用一次验证方法?当我尝试它时,它在 messages IList 中给出了这个错误:

Invalid type. Expected Object but got Array. Path ", line1, position 1."

最佳答案

解决方案是创建一个对象并将数组放入其中。

var housesObject = new {
 houses = houses
};

string housesJsonString = JsonConvert.SerializeObject(housesObject);
JObject housesJson = JObject.Parse(housesJsonString);
IList < string > messages;
bool valid = housesJson.IsValid(schema, out messages);
return valid;

关于c# - Json.NET 根据 Schema 验证 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42190942/

相关文章:

时间:2019-03-17 标签:c#jsoncountnodeschildren

java - 验证架构是有效的 JSON 架构而不是数据

c# - Swift:将 NSDate 转换为 C# ticks

c# - Azure 函数获取登录标识

c# - asp.net core中间件无法捕获newtonsoft异常

c# - WebAPI DTO 响应请求消息传递模式

gradle - 运行时无法在 build.gradle 中包含 JSON Schema 验证 json 文件

通过注释将 Java 类转换为 JSON 自定义架构

c# - 序列化包含 BitmapImage 的对象

c# - 有没有办法发出 System.Threading.Tasks.Task 完成的信号?