Json.NET针对Schema验证JSON数组

时间:2017-02-12 17:38:39

标签: c# json.net jsonschema

我想验证一个有数组的模式,一次调用validate方法。我是在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中出现了这个错误:

  

无效的类型。预期的对象但得到了数组。路径&#34;,第1行,位置   1&#34;

1 个答案:

答案 0 :(得分: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;
相关问题