How do I serialize a nested dataset using ASP.NET and Json.net?

I am currently using json.net to serialize dataset to 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 (output)

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

      


Desired exit

I have created a separate stored procedure to populate nested data and am trying to figure out how to iterate through the rows of dataset / json and insert nested data into each item.

In .net, I would just loop the element for each

.

Json

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

      

Suggestions can be in C#

orVB

+3


source to share





All Articles