在 Visual Basic 中验证 JSON 文件

Validate JSON file in Visual Basic

我正在尝试验证 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

Some websites可以帮你把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)

开始你的“C# for VB Devs cheat sheet”:

Variable declaration - developer declares type

C#: Type name
VB: Dim name as Type

Variable declaration - compiler infers type

C#: var name
VB: Dim name

Variable assignment

C#: name = ...
VB: name = ...

End of statement

C#: ;
VB: <CRLF>

Continuation of statement across lines

C#: <CRLF>
VB: _<CRLF>