【问题标题】:Validate JSON file in Visual Basic在 Visual Basic 中验证 JSON 文件
【发布时间】:2021-06-10 03:29:51
【问题描述】:

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

【问题讨论】:

  • Dim schema as JsonSchema = JsonSchema.Parse(schemaJson)

标签: json vb.net visual-studio json.net schema


【解决方案1】:

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)

【讨论】:

    【解决方案2】:

    开始您的“C# for VB Devs 备忘单”:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多