0

I am currently using the Newtonsoft framework to serialize my product categories dataset into json data.

The current way i do it is:

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

This works well and produces results like this:

{
  "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"
    }
  ]
}

Now what I would like to do is insert the sub categories into the json output. I can obtain the sub category data easily enough and put it into a dataset but what is the best method to have an array of objects inside the current object. The output should look like this:

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

Any thoughts and suggestions how i would serialise a linked datatable inside a dataset?

1
  • 1
    One way of doing it would be to create a class Table which contains a List<SubCategories>. Convert your dataset to this class and serialize this class to json. Commented Dec 9, 2016 at 9:15

2 Answers 2

2

It was Carra's answer that lead me to this but in case anyone wants to see the final code:

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

Please note that the function getDataSetFromSql is just a helper function I've created to help me quickly obtain stored procedure datasets from SQL

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help :)
1

You can do it like this:

  • Create a class Table which contains id, name, urlfriendlyname and a List properties.
  • Convert your dataset to this table object.
  • Serialize this table object to json.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.