将C#datatable转换为分层JSON数据

时间:2018-01-18 23:31:13

标签: c# json linq datatable json.net

我得到一个C#DataTable。它有几行和几列。列数是动态的,但通常在4-8列之间。

DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("America");
            dataTable.Columns.Add("Japan");
            dataTable.Columns.Add("Singapore");

            dataTable.Rows.Add("A", 200, 100, 300);
            dataTable.Rows.Add("B", 300, 300, 600);
            dataTable.Rows.Add("C", 400, 400, 700);
            dataTable.Rows.Add("D", 500, 500, 800);
            dataTable.Rows.Add("E", 600, 600, 900);
            dataTable.Rows.Add("F", 700, 700, 1000);
            dataTable.Rows.Add("G", 800, 800, 600);
            dataTable.Rows.Add("H", 900, 100, 400);
            dataTable.Rows.Add("I", 100, 200, 300);
            dataTable.Rows.Add("J", 200, 300, 200);
            dataTable.Rows.Add("K", 300, 500, 500);

行与它们之间有关系。我有另一个数据表告诉我们DataTable中哪一行是父行,哪一行是子行。

  DataTable mappingTable = new DataTable();
        mappingTable.Columns.Add("Name");
        mappingTable.Columns.Add("id");
        mappingTable.Columns.Add("parentID");


        mappingTable.Rows.Add("A", 1, null);
        mappingTable.Rows.Add("B", 2, 1);
        mappingTable.Rows.Add("C", 3, 1);
        mappingTable.Rows.Add("D", 4, null);
        mappingTable.Rows.Add("E", 5, 4);
        mappingTable.Rows.Add("F", 6, null);
        mappingTable.Rows.Add("G", 7, 6);
        mappingTable.Rows.Add("H", 8, 6);
        mappingTable.Rows.Add("I", 9, 1);
        mappingTable.Rows.Add("J", 10 ,null);
        mappingTable.Rows.Add("K", 11, 10);

我使用以下代码将第一个DataTable转换为JSON:

var json = Newtonsoft.Json.JsonConvert.SerializeObject(dataTable);



 [
  {
    "name": "A",
    "america": "200",
    "japan": "100",
    "singapore": "300"
  },
  {
    "name": "B",
    "america": "300",
    "japan": "300",
    "singapore": "600"
  },
  {
    "name": "C",
    "america": "400",
    "japan": "400",
    "singapore": "700"
  },
  {
    "name": "D",
    "america": "500",
    "japan": "500",
    "singapore": "800"
  },
  {
    "name": "E",
    "america": "600",
    "japan": "600",
    "singapore": "900"
  },
  {
    "name": "F",
    "america": "700",
    "japan": "700",
    "singapore": "1000"
  },
  {
    "name": "G",
    "america": "800",
    "japan": "800",
    "singapore": "600"
  },
  {
    "name": "H",
    "america": "900",
    "japan": "100",
    "singapore": "400"
  },
  {
    "name": "I",
    "america": "100",
    "japan": "200",
    "singapore": "300"
  },
  {
    "name": "J",
    "america": "200",
    "japan": "300",
    "singapore": "200"
  },
  {
    "name": "K",
    "america": "300",
    "japan": "500",
    "singapore": "500"
  }
]

现在我正在考虑解析这个JSON,然后在它上面运行LINQ&使用MappingTanle生成分层JSON。

当我使用这一行时,我收到错误:

JObject rss = JObject.Parse(json);

错误:base {"从JsonReader读取JObject时出错。当前的JsonReader项不是对象:StartArray。路径'',第1行,第1位。"} System.Exception {Newtonsoft.Json.JsonReaderException}

This is the output I am trying to generate:
[{
        "name": "A",
        "america": "200",
        "japan": "100",
        "singapore": "300",
        "children": [{
                "name": "B",
                "america": "300",
                "japan": "300",
                "singapore": "600"
            }, {
                "name": "C",
                "america": "400",
                "japan": "400",
                "singapore": "700"
            }, {
                "name": "I",
                "america": "100",
                "japan": "200",
                "singapore": "300"
            }
        ]
    }, {
        "name": "D",
        "america": "500",
        "japan": "500",
        "singapore": "800",
        "children": [{
                "name": "E",
                "america": "600",
                "japan": "600",
                "singapore": "900"
            }
        ]
    }, {
        "name": "F",
        "america": "700",
        "japan": "700",
        "singapore": "1000",
        "children": [{
                "name": "G",
                "america": "800",
                "japan": "800",
                "singapore": "600"
            }, {
                "name": "H",
                "america": "900",
                "japan": "100",
                "singapore": "400"
            }
        ]
    }, {
        "name": "J",
        "america": "200",
        "japan": "300",
        "singapore": "200",
        "children": [{
                "name": "K",
                "america": "300",
                "japan": "500",
                "singapore": "500"
            }
        ]
    }
]

0 个答案:

没有答案