2

DataTable2 つの s のリストがあります。このリストを JSON 配列に変換していますDataTableが、JSON 文字列のすべてにテーブル名を追加する必要があります。

どうやってやるの?

これは私がリストを変換する方法です:

Dim json As String = JsonConvert.SerializeObject(list, New DataTableConverter())

望ましい JSON 出力:

{
    "category": [
        {
            "id": "1",
            "desc": "default",
        },
        {
            "id": "2",
            "desc": "fun",
        }
    ],
    "images": [
        {
            "image ID": "1",
            "link": "images/logo.jpg"
            "category": "1"
        },
        {
            "image ID": "2",
            "link": "images/logo2.jpg"
            "category": "2"
        }
    ]
}
4

1 に答える 1

2

Try putting your tables into a DataSet instead of a list, then serialize the DataSet.

Dim table1 As New DataTable("category")
table1.Columns.Add("id", GetType(String))
table1.Columns.Add("desc", GetType(String))
table1.Rows.Add("1", "default")
table1.Rows.Add("2", "fun")

Dim table2 As New DataTable("images")
table2.Columns.Add("image ID", GetType(String))
table2.Columns.Add("link", GetType(String))
table2.Columns.Add("category", GetType(String))
table2.Rows.Add("1", "images/logo.jpg", "1")
table2.Rows.Add("2", "images/logo2.jpg", "2")

Dim dataSet As New DataSet()
dataSet.Tables.Add(table1)
dataSet.Tables.Add(table2)

Dim json As String = JsonConvert.SerializeObject(dataSet, Formatting.Indented)

Console.WriteLine(json)

Output:

{
  "category": [
    {
      "id": "1",
      "desc": "default"
    },
    {
      "id": "2",
      "desc": "fun"
    }
  ],
  "images": [
    {
      "image ID": "1",
      "link": "images/logo.jpg",
      "category": "1"
    },
    {
      "image ID": "2",
      "link": "images/logo2.jpg",
      "category": "2"
    }
  ]
}
于 2013-10-24T08:13:20.800 に答える