C# 以编程方式从JSON数据生成JSON模式

C# 以编程方式从JSON数据生成JSON模式,c#,json,schema,C#,Json,Schema,我想从json数据文件生成json模式,如下所示。(这是通过在线工具生成的) 使用JSON.NET或任何其他工具都可以吗 Json数据文件: { "graph": [ { "id": 453, "cid": 143, "title": "graph1", "description": "", "thumbnailPath": "art.jpg", "detailPath": "art.jpg", "l

我想从json数据文件生成json模式,如下所示。(这是通过在线工具生成的) 使用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"
          }
        }
      }
    }
  }
}
我的目标是不使用工具,因为我希望在程序运行时生成它。

好的,我这样做了:

  • 下载VisualStudio扩展
  • 将.vsix文件重命名为.zip
  • 将zip文件解压缩到任意文件夹
  • 请参考Microsoft.Json.SchemaExtension.dll和Microsoft.Json.SchemaProducer.dll
  • 确保您的项目是一个.NETFramework4.5项目(因为这两个程序集是使用.NET4.5构建的,所以我没有在网络上的其他地方找到这些程序集)
  • 使用此代码从JSON获取JSON模式
  • 虽然我不能向你保证这种方法是合法的
  • -


    谢谢你的回答,但是在你的解决方案的第一部分,有一个我们没有的代码。(在string schemaJson=…)之前,理论上我们只有一个众所周知的json文件,我们想要得到它的json模式。谢谢@Michal Hainc,我认为你的方法是一种解决方案,但不幸的是,我使用Xamarin和我的项目使用.NET 2。试试这个工具。它基于查询模式生成json。
    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);