简体   繁体   English

json使用Powershell中的json模式进行验证

[英]json validate with json schema in powershell

Here is my code to validate json with jsonschema in powershell. 这是我的代码,用于在Powershell中使用jsonschema验证json。 and its error ed with following error message. 和它的错误与以下错误消息。

Cannot find an overload for "Validate" and the argument count: "2". 找不到“ Validate”和参数计数:“ 2”的重载。

$json =   Get-Content -Raw -Path C:\temp\locationstores.json
$SchmeaJson = Get-Content -Raw -Path C:\temp\LocationstoreSchema.json
NewtonsoftJsonAssembly =  [Reflection.Assembly]::LoadFile("C:\temp\Json100r3\Bin\net45\Newtonsoft.Json.dll")
$NewtonsoftJsonSchemaAssembly =  [Reflection.Assembly]::LoadFile("C:\temp\JsonSchema30r3\Bin\net45\NewtonSoft.Json.Schema.dll")
Add-Type -Path "C:\temp\JsonSchema30r3\Bin\net45\NewtonSoft.Json.Schema.dll" 
Add-Type -Path "C:\temp\Json100r3\Bin\net45\Newtonsoft.Json.dll"
[Newtonsoft.Json.Schema.SchemaExtensions]::Validate([Newtonsoft.Json.Linq.JToken]::Parse($json), [Newtonsoft.Json.Schema.JSchema]::Parse($SchmeaJson))

Please can some one let me know what is the error and any other way to validate json with json schema in powershell.. 请让我知道错误是什么,以及使用powershell中的json模式验证json的任何其他方法。

You could try an alternative method call: 您可以尝试其他方法调用:

Example C# code to adapt: 示例C#代码以适应:

public void ValidateWithOutEventHandlerFailure()
{
        //...
        JsonSchema schema = JsonSchema.Parse("{'pattern':'lol'}");
        JToken stringToken = JToken.FromObject("pie");
        stringToken.Validate(schema);

Source: JSON.NET unit tests at https://github.com/JamesNK/Newtonsoft.Json/blob/aa5f28c09732db5fb3e316433547ea7cdecd8804/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs 来源: https ://github.com/JamesNK/Newtonsoft.Json/blob/aa5f28c09732db5fb3e316433547ea7cdecd8804/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs上的JSON.NET单元测试

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

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