简体   繁体   English

根据 .NET 中的模式验证 JSON

[英]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.在您的解决方案中添加 Newtonsoft 的 Json NuGet 包。 Add below function and pass Schema and your json response in string to below function.添加以下函数并将 Schema 和您的 json 响应以字符串形式传递给下面的函数。

  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. Json Everything及其前身Manatee.Json非常好而且速度很快。

  • NJsonSchema comfortable api however too slow for our use case (schema closing to 100kb the json in 10s of kbs); NJsonSchema舒适的 api 但是对于我们的用例来说太慢了(模式接近 100kb 的 json 在 10s 的 kbs 中); the above mentioned Manatee and json-everything have a "flag-only" validation mode which is missing here上面提到的 Manatee 和 json-everything 有一个“仅标志”验证模式,这里没有

  • Newtonsoft (Paid) i have not checked this one Newtonsoft (付费)我没有检查过这个

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

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