从.net数据集中添加JSON内的数组

时间:2016-12-09 09:11:01

标签: sql json vb.net dataset

我目前正在使用Newtonsoft框架将我的产品类别数据集序列化为json数据。

我目前的做法是:

Public Function Category() As String
        Try
            Dim ds As DataSet = getDataSetFromPTLSAGE("website.CategoryList", db_conx("xxxxxxxxxxxx"))
            Dim string_ As String
            string_ = JsonConvert.SerializeObject(ds, Newtonsoft.Json.Formatting.Indented)
            Return string_
        Catch ex As Exception
            Return ex.ToString
        End Try

    End Function

这很好用并产生如下结果:

{
  "Table": [
    {
      "Id": "21",
      "Name": "Accessories",
      "URLFriendlyName": "accessories"
    },
    {
      "Id": "06",
      "Name": "Baby",
      "URLFriendlyName": "baby"
    },
    {
      "Id": "01",
      "Name": "Bath & Shower",
      "URLFriendlyName": "bath-shower"
    },
    {
      "Id": "18",
      "Name": "Books & Stationery",
      "URLFriendlyName": "books-stationery"
    }
  ]
}

现在我想要做的是将子类别插入到json输出中。我可以很容易地获得子类别数据并将其放入数据集中,但是在当前对象中包含对象数组的最佳方法是什么。输出应如下所示:

{
  "Table": [
    {
      "Id": "21",
      "Name": "Accessories",
      "URLFriendlyName": "accessories",
      "SubCategory": [
                     {
                          "Id":"01",
                          "Name":"Travel",
                          "URLFriendlyName":"travel"
                     },
                     {
                           "Id":"02",
                           "Name":"Umbrella",
                           "URLFriendlyName":"umbrella"
                     }
                     ]
    }
   ]
}

如何在数据集中序列化链接数据表的任何想法和建议?

2 个答案:

答案 0 :(得分:2)

Carra的回答引导我这个,但万一有人想看到最终的代码:

Public Class Class1
    Public Function Category() As String
        Try
            Dim _categorylist As New CategoryList
            _categorylist.Categories = New List(Of Categories)
            Dim ds As DataSet = getDataSetFromSQL("website.CategoryList", db_conx("xxxxxxxx"))
            If ds.Tables(0).Rows.Count > 0 Then
                For i = 0 To ds.Tables(0).Rows.Count - 1
                    Dim _category As New Categories
                    Dim id As String = ds.Tables(0).Rows(i)("Id").ToString.Trim
                    _category.Id = id
                    _category.Name = ds.Tables(0).Rows(i)("Name").ToString.Trim
                    _category.URLFriendlyName = ds.Tables(0).Rows(i)("URLFriendlyName").ToString.Trim
                    _category.SubCategories = New List(Of SubCategories)
                    Dim subDs As DataSet = getDataSetFromSQL("website.SubCategoryList", db_conx("xxxxxxxx"), "@id", id)
                    If subDs.Tables(0).Rows.Count > 0 Then
                        For x = 0 To subDs.Tables(0).Rows.Count - 1
                            Dim _subCategory As New SubCategories
                            _subCategory.Id = subDs.Tables(0).Rows(x)("Id").ToString.Trim
                            _subCategory.Name = subDs.Tables(0).Rows(x)("Name").ToString.Trim
                            _subCategory.URLFriendlyName = subDs.Tables(0).Rows(x)("URLFriendlyName").ToString.Trim
                            _category.SubCategories.Add(_subCategory)
                        Next x
                    End If
                    _categorylist.Categories.Add(_category)
                Next
            End If
            Return JsonConvert.SerializeObject(_categorylist, Newtonsoft.Json.Formatting.Indented)
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
End Class

Public Class CategoryList
    Public Property Categories() As List(Of Categories)
End Class

Public Class Categories
    Public Property Id() As String
    Public Property Name() As String
    Public Property URLFriendlyName() As String
    Public Property SubCategories As List(Of SubCategories)
End Class

Public Class SubCategories
    Public Property Id() As String
    Public Property Name() As String
    Public Property URLFriendlyName() As String
End Class

请注意,函数getDataSetFromSql只是我创建的一个辅助函数,可以帮助我从SQL快速获取存储过程数据集

答案 1 :(得分:1)

你可以这样做:

  • 创建一个包含id,name,urlfriendlyname和List属性的类。
  • 将数据集转换为此表格对象。
  • 将此表对象序列化为json。