首页 > 解决方案 > 在 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

标签: jsonvb.netvisual-studiojson.netschema

解决方案


一些网站可以帮助您将 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)

推荐阅读