如何使用 VB.Net 中的架构验证 JSON 文件?
How Do I Validate a JSON file With a Schema in VB.Net?
我正在尝试验证 VB.net 上的一些 JSON 文件。
但是,每当我 运行 我的代码卡在
Dim Schema As JsonSchema = JsonSchema.Parse(SchemaString)
错误说
An unhandled exception of type 'Newtonsoft.Json.JsonException' occurred in Newtonsoft.Json.dll.
还有一条警告说 JSON 验证已移动到它自己的包中。所以,我很确定我只是导入了错误的包,但我不确定。
如果有人能指出正确的方向,我将不胜感激,
谢谢。
这是我的 VB.net 代码
Imports System
Imports Newtonsoft.Json.Schema
Imports Newtonsoft.Json.Linq
Public Function Validate_JSON()
Dim SplunkPath As String = "Z:\Database Project\Splunk Folder\Dropbox\Splunk_File.json"
Dim SchemaPath As String = "Z:\Database Project\Schema Folder\Schema_File.json"
Dim Schema_String As String = My.Computer.FileSystem.ReadAllText(SchemaPath)
Dim Schema As JsonSchema = JsonSchema.Parse(Schema_String)
Dim Data_String As String = My.Computer.FileSystem.ReadAllText(SplunkPath)
Dim Data_JSON As JObject = JObject.Parse(Data_String)
Dim Splunk_Status As Boolean = Data_JSON.IsValid(Schema)
Return 0
End Function
这里是Splunk_File.json
{
"Site": "USI",
"SN": "21165",
"MN": "F2C00W",
"DateTime": "05/18/2021"
}
这里是Schema_File.json
{
"$schema" :"http://json-schema.org/draft-04/schema",
"properties": {
"$schema": "#",
"Site": {
"type": "string"
},
"SN": {
"Type": "string"
},
"MN": {
"Type": "string"
}
}
}
$schema
仅在根目录下有效,并且 properties
值必须是模式。
你在 properties
里面有一个 "$schema" : "#"
。这意味着您试图说您的 JSON 对象应该有一个名为 属性 的模式,它可以根据模式 #
进行验证。但是 #
不是有效的架构对象,因此解析失败。
您需要从 properties
中删除 $schema
。
我还建议使用架构规范的较新草案(如果您可以控制架构)。草案 6 是与最新版本 2020-12 兼容的最旧版本。
但是为此您可能需要使用不同的验证包。有几个可用。我的是 JsonSchema.Net.
我正在尝试验证 VB.net 上的一些 JSON 文件。 但是,每当我 运行 我的代码卡在
Dim Schema As JsonSchema = JsonSchema.Parse(SchemaString)
错误说
An unhandled exception of type 'Newtonsoft.Json.JsonException' occurred in Newtonsoft.Json.dll.
还有一条警告说 JSON 验证已移动到它自己的包中。所以,我很确定我只是导入了错误的包,但我不确定。
如果有人能指出正确的方向,我将不胜感激,
谢谢。
这是我的 VB.net 代码
Imports System
Imports Newtonsoft.Json.Schema
Imports Newtonsoft.Json.Linq
Public Function Validate_JSON()
Dim SplunkPath As String = "Z:\Database Project\Splunk Folder\Dropbox\Splunk_File.json"
Dim SchemaPath As String = "Z:\Database Project\Schema Folder\Schema_File.json"
Dim Schema_String As String = My.Computer.FileSystem.ReadAllText(SchemaPath)
Dim Schema As JsonSchema = JsonSchema.Parse(Schema_String)
Dim Data_String As String = My.Computer.FileSystem.ReadAllText(SplunkPath)
Dim Data_JSON As JObject = JObject.Parse(Data_String)
Dim Splunk_Status As Boolean = Data_JSON.IsValid(Schema)
Return 0
End Function
这里是Splunk_File.json
{
"Site": "USI",
"SN": "21165",
"MN": "F2C00W",
"DateTime": "05/18/2021"
}
这里是Schema_File.json
{
"$schema" :"http://json-schema.org/draft-04/schema",
"properties": {
"$schema": "#",
"Site": {
"type": "string"
},
"SN": {
"Type": "string"
},
"MN": {
"Type": "string"
}
}
}
$schema
仅在根目录下有效,并且 properties
值必须是模式。
你在 properties
里面有一个 "$schema" : "#"
。这意味着您试图说您的 JSON 对象应该有一个名为 属性 的模式,它可以根据模式 #
进行验证。但是 #
不是有效的架构对象,因此解析失败。
您需要从 properties
中删除 $schema
。
我还建议使用架构规范的较新草案(如果您可以控制架构)。草案 6 是与最新版本 2020-12 兼容的最旧版本。
但是为此您可能需要使用不同的验证包。有几个可用。我的是 JsonSchema.Net.