json - 在 Visual Basic 中验证 JSON 文件
问题描述
我正在尝试在 Visual Basic 代码中验证 JSON 文件。我一直在寻找有关 Newtonsoft 的文档,但是它们仅提供 C# 中的示例代码。我基本上想使用模式字符串来验证 VB 数据库中的一些 JSON 文件。如果下面的代码(用 C# 编写)是用 VB 编写的,它会是什么样子?
你如何
JsonSchema schema = JsonSchema.Parse(schemaJson);
在 Visual Basic 代码中编写代码?
string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties':
{
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}";
JsonSchema schema = JsonSchema.Parse(schemaJson);
JObject person = JObject.Parse(@"{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}");
bool valid = person.IsValid(schema);
// true
解决方案
一些网站可以帮助您将 C# 代码转换为 VB.NET。
这是转换的结果:
Dim schemaJson As String = "{
'description': 'A person',
'type': 'object',
'properties':
{
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}"
Dim schema As JsonSchema = JsonSchema.Parse(schemaJson)
Dim person As JObject = JObject.Parse("{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}")
Dim valid As Boolean = person.IsValid(schema)
推荐阅读
- reactjs - 如何在本机反应中显示作为对象传递到另一个屏幕的状态
- logstash-grok - 包含小括号的字符串的 Logstash Grok 模式
- javascript - 如何从Vue中的数组中获取成本最低的元素
- spring-boot - Hibernate Validator with Hibernate Dynamic Models (No Entity Class)
- apache-spark - Spark shell 未从 spark-cassandra-connector 3.1.0 开始
- flutter - Flutter 如何在另一个页面上显示对话框
- python - 为什么将 python 命令分配给 sh var?这个文件有什么作用?
- c# - AutoMapper 和 .NET Core:缺少类型映射配置或不支持的映射
- laravel - 在 API 路由中使用 Auth 外观(Laravel 8)
- excel - 在 Excel 中使用带有 filter() 函数的下拉选择