简体   繁体   中英

Validate JSON against a schema in .NET

我知道有一个 JSON 模式验证的提议标准,.NET 中是否有实现?

Json.NET 的免费开源替代方案是NJsonSchema (JSON Schema Draft 4)。

Json.NET有这个功能。

Add Newtonsoft's Json NuGet Package in your solution. Add below function and pass Schema and your json response in string to below function.

  public void ValidateSchema(JsonSchema JSchema, string JsonString)  {
        JsonString = JsonString.Replace("\"", "'");
        var ArrJobj = JArray.Parse(JsonString);

        foreach (JObject jo in ArrJobj)
        {
            if (!jo.IsValid(JSchema)) throw new Exception("Schems Validation failed");

        }

    }

Hope this helps

  • Json Everything and its predecesor Manatee.Json are quite good and fast.

  • NJsonSchema comfortable api however too slow for our use case (schema closing to 100kb the json in 10s of kbs); the above mentioned Manatee and json-everything have a "flag-only" validation mode which is missing here

  • Newtonsoft (Paid) i have not checked this one

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