简体   繁体   中英

how to validate Jarray response data schema

I have a json schema validate function something like following

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

[
    {
        "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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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