【问题标题】:Generate JSON Schema from JSON Data Programmatically以编程方式从 JSON 数据生成 JSON 模式
【发布时间】:2015-07-29 04:32:21
【问题描述】:

我想从 json 数据文件生成 json 模式,如下所示。(这是使用在线工具生成的,http://jsonschema.net/) 用 JSON.NET 或其他任何方法都可以吗?

Json 数据文件:

{
  "graph": [
    {
      "id": 453,
      "cid": 143,
      "title": "graph1",
      "description": "",
      "thumbnailPath": "art.jpg",
      "detailPath": "art.jpg",
      "link": "www.test.html",
      "author": "graph Team"
    },
    {
      "id": 12,
      "cid": 121,
      "title": "graph2",
      "description": "",
      "thumbnailPath": "art2.jpg",
      "detailPath": "art2.jpg",
      "link": "www.test2.html",
      "author": "graph Team"
    }
  ]
}

输出:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "/",
  "type": "object",
  "properties": {
    "graph": {
      "id": "graph",
      "type": "array",
      "items": {
        "id": "1",
        "type": "object",
        "properties": {
          "id": {
            "id": "id",
            "type": "integer"
          },
          "cid": {
            "id": "cid",
            "type": "integer"
          },
          "title": {
            "id": "title",
            "type": "string"
          },
          "description": {
            "id": "description",
            "type": "string"
          },
          "thumbnailPath": {
            "id": "thumbnailPath",
            "type": "string"
          },
          "detailPath": {
            "id": "detailPath",
            "type": "string"
          },
          "link": {
            "id": "link",
            "type": "string"
          },
          "author": {
            "id": "author",
            "type": "string"
          }
        }
      }
    }
  }
}

我的目标不是使用工具,因为我想在程序运行时生成它。

【问题讨论】:

标签: c# json schema


【解决方案1】:

好的,我这样做了:

  1. 下载Visual Studio扩展https://visualstudiogallery.msdn.microsoft.com/b4515ef8-a518-41ca-b48c-bb1fd4e6faf7
  2. 将 .vsix 文件重命名为 .zip
  3. 将 zip 文件解压到任意文件夹
  4. 引用 Microsoft.Json.SchemaExtension.dll 和 Microsoft.Json.SchemaProducer.dll
  5. 确保您的项目是一个 .net 框架 4.5 项目(因为这 2 个程序集是使用 .net 4.5 构建的,我没有在网上的其他地方找到这些程序集)
  6. 使用此代码从您的 JSON 中获取 JSON 架构
  7. 虽然我不能向你保证这种方法是合法的

-

string json = @"{
  ""graph"": [
    {
      ""id"": 453,
      ""cid"": 143,
      ""title"": ""graph1"",
      ""description"": """",
      ""thumbnailPath"": ""art.jpg"",
      ""detailPath"": ""art.jpg"",
      ""link"": ""www.test.html"",
      ""author"": ""graph Team""
    },
    {
      ""id"": 12,
      ""cid"": 121,
      ""title"": ""graph2"",
      ""description"": """",
      ""thumbnailPath"": ""art2.jpg"",
      ""detailPath"": ""art2.jpg"",
      ""link"": ""www.test2.html"",
      ""author"": ""graph Team""
    }
  ]
}";
string jsonSchema = Microsoft.Json.SchemaProducer.SchemaBuilder.CreateSchema(json);

【讨论】:

  • 感谢您的回答,但在您的解决方案的第一部分中,有一个我们没有的代码。 (在 string schemaJson=...) 之前,理论上我们只有一个众所周知的 json 文件,我们想要得到它 Json Schema。
  • 感谢@Michal Hainc,我认为你的方法是一种解决方案,但不幸的是我在 Xamarin 上工作,我的项目使用 .NET 2。
猜你喜欢
  • 2020-06-19
  • 2016-05-15
  • 2017-02-22
  • 1970-01-01
  • 2011-11-12
  • 2019-06-23
  • 2018-06-30
  • 1970-01-01
相关资源
最近更新 更多