简体   繁体   English

如何验证 Jarray 响应数据模式

[英]how to validate Jarray response data schema

I have a json schema validate function something like following我有一个 json 架构验证 function 类似于以下内容

private void ValidateJsonSchema<T>(string jsonData)
    {
        var schema = JsonSchema.FromType<T>();
        var errors = schema.Validate(jsonData);
    }

the param jsonData is the content of the response, but there is sometimes it will be an array like following参数 jsonData 是响应的内容,但有时它会是一个数组,如下所示

[
    {
        "id": "123",
        "title": "abc"
    },
    {
        "id": "456",
        "title": "def"
    }
]

so how can I handle this?那么我该如何处理呢?

I tried following code and it works:我尝试了以下代码并且它有效:

        var schema = JsonSchema.FromType(type);
        var jtoken = JToken.Parse(jsonData);
        if (jtoken.Type == JTokenType.Array)
            schema.Type = JsonObjectType.Array;
        var errors = schema.Validate(jtoken);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM