如何使用ASP.NET和Json.net序列化嵌套数据集?

时间:2017-04-11 15:34:17

标签: c# asp.net json vb.net json.net

目前我正在使用json.net将数据集序列化为json。

VB.NET

<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Sub GetData()
  Dim json As String

  Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("Con").ConnectionString)
    Using cmd As New SqlCommand("sp", con)
      cmd.CommandType = CommandType.StoredProcedure    
      Using sda As New SqlDataAdapter(cmd)
        Using ds As New DataSet()
          sda.Fill(ds)
          json = JsonConvert.SerializeObject(ds, Formatting.Indented)    
        End Using
      End Using    
    End Using
  End Using

  Context.Response.ContentType = "application/json; charset=utf-8"
  Context.Response.Write(json)
End Sub

JSON (输出)

{
  "Table": [
    {
      "Name": "Head1",
      "Number": 0
    },
    {
      "Name": "Head1",
      "Number": 1
    },
    {
      "Name": "Head2",
      "Number": 0
    }
  ]
}

期望的输出

我创建了一个单独的存储过程来填充嵌套数据,并试图找出如何遍历数据集/ json的行并将嵌套数据插入到每个项目中。

在.net中我只会做一个for each项循环。

JSON

{
  "Table": [
    {
      "Name": "Head1"
      "NumberSection": [
        "Number": 0,
        "Number": 1
      ]
    },
    {
      "Name": "Head2"
      "NumberSection": [
        "Number": 0
      ]
    }
  ]
}

建议可以在C#VB

0 个答案:

没有答案