VB.net如何序列化json对象的列表/数组

时间:2015-07-18 05:47:36

标签: arrays json vb.net list serialization

我有一个问题,我想序列化对象列表(对象列在'列表'中,我称之为'项目')成为json,但我不知道#39;知道我需要使用newtonsoft.json进行以下哪些类/结构 串行器:

 {
    "List": [
        {
            "name": "item1"
        },
        {
            "name": "item2"
        },
        {
            "name": "item3"
        }
    ],
    "Property1": "value",
    "Property2": "value"
}

有人知道这个问题的解决方案吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

这是我的嵌套json序列化类。

我认为这个例子会帮助你

Public Class MainCatList
Public Property Categorylist() As List(Of MainCategory)
End Class

Imports Newtonsoft.Json
Imports System
Imports System.Runtime.CompilerServices

<JsonObject(MemberSerialization.OptIn)>
Public Class MainCategory
<JsonProperty(PropertyName:="Ana Katagori Kodu")>
Public Property AnaKatagoriKodu() As String

<JsonProperty(PropertyName:="Ana Katagori Adı")>
Public Property AnaKatagoriAdi() As String

<JsonProperty(PropertyName:="Marka")>
Public Property Marka() As String

<JsonProperty(PropertyName:="Ana Ürün Kodu")>
Public Property AnaUrunKodu() As String

<JsonProperty(PropertyName:="Ana Ürün Adı")>
Public Property AnaUrunadi() As String

<JsonProperty(PropertyName:="Ana Ürün Açıklaması")>
Public Property AnaUrunAciklamasi() As String

<JsonProperty(PropertyName:="Miktar")>
Public Property Miktar() As Integer

<JsonProperty(PropertyName:="Alt Kırılımlar")>
Public Property DeepCategories As List(Of DeepCategori)

Public Sub New()
    MyBase.New()
    Me.AnaKatagoriKodu = ""
    Me.AnaKatagoriAdi = ""
    Me.Marka = ""
    Me.AnaUrunKodu = ""
    Me.AnaUrunadi = ""
    Me.AnaUrunAciklamasi = ""
    Me.Miktar = 0
    Me.DeepCategories = New List(Of DeepCategori)()
End Sub

Imports Newtonsoft.Json
Imports System
Imports System.Runtime.CompilerServices

<JsonObject(MemberSerialization.OptIn)>
Public Class DeepCategori
<JsonProperty(PropertyName:="Katagori Kodu")>
Public Property KategoriKodu As String

<JsonProperty(PropertyName:="Katagori")>
Public Property Kategori As String

<JsonProperty(PropertyName:="Renk")>
Public Property Renk As String

<JsonProperty(PropertyName:="Beden")>
Public Property Beden As String

<JsonProperty(PropertyName:="Barkod")>
Public Property Barkod As String

<JsonProperty(PropertyName:="Miktar")>
Public Property Miktar As String

<JsonProperty(PropertyName:="Ürün Kodu")>
Public Property UrunKodu As String

<JsonProperty(PropertyName:="Ürün Adı")>
Public Property UrunAdi As String

<JsonProperty(PropertyName:="Fiyat")>
Public Property Fiyat As String


Public Sub New()
    MyBase.New()
    Me.KategoriKodu = ""
    Me.Kategori = ""
    Me.Renk = ""
    Me.Beden = ""
    Me.Barkod = ""
    Me.Miktar = 0
    Me.UrunKodu = ""
    Me.UrunAdi = ""
    Me.Fiyat = 0
End Sub


End Class

Try
            Dim constr As New SqlConnectionStringBuilder
            With constr
                .DataSource = "."
                .InitialCatalog = "YourDb"
                .UserID = "Your User ID"
                .Password = "Your Password"

            End With


            Dim ds As New DataTable
            Using da As New SqlDataAdapter("Your SQL Command", constr.ConnectionString)

                da.Fill(ds)
                Dim subDs As New DataSet
                da.SelectCommand.CommandText = "Your SQL Command Text"
                da.Fill(subDs)
                Dim foundRows() As Data.DataRow
                Dim _categorylist As New MainCatList
                _categorylist.Categorylist = New List(Of MainCategory)

                If ds.Rows.Count > 0 Then

                    For i = 0 To ds.Rows.Count - 1
                        Dim _category As New MainCategory
                        Dim id As String = ds.Rows(i)("Ana Katagori Kodu").ToString.Trim

                        _category.AnaKatagoriKodu = id
                        _category.AnaKatagoriAdi = ds.Rows(i)("Ana Katagori Adı").ToString.Trim
                        _category.AnaUrunAciklamasi = ds.Rows(i)("Ana Ürün Açıklaması").ToString.Trim
                        _category.AnaUrunadi = ds.Rows(i)("Ana Ürün Adı").ToString.Trim
                        _category.Marka = ds.Rows(i)("Marka").ToString.Trim
                        _category.Miktar = ds.Rows(i)("Miktar").ToString.Trim
                        _category.AnaUrunKodu = ds.Rows(i)("Ana Ürün Kodu").ToString.Trim

                        _category.DeepCategories = New List(Of DeepCategori)




                        foundRows = subDs.Tables(0).Select("[Ana Katagori Kodu]  =  '" & id & "'")



                        If foundRows.Length - 1 > 0 Then


                            For x = 0 To foundRows.Length - 1
                                Dim _subCategory As New DeepCategori
                                _subCategory.KategoriKodu = foundRows(x).Item(0).ToString.Trim
                                _subCategory.Kategori = foundRows(x).Item(3).ToString.Trim ' subDs.Rows(x)("Katagori").ToString.trim.
                                _subCategory.Renk = foundRows(x).Item(8).ToString.Trim 'subDs.Rows(x)("Renk").ToString.trim.
                                _subCategory.Beden = foundRows(x).Item(9).ToString.Trim ' subDs.Rows(x)("Beden").ToString.trim.
                                _subCategory.Barkod = foundRows(x).Item(10).ToString.Trim ' subDs.Rows(x)("Barkod").ToString.trim.
                                _subCategory.Miktar = foundRows(x).Item(11).ToString.Trim ' subDs.Rows(x)("Miktar").ToString.trim.
                                _subCategory.UrunKodu = foundRows(x).Item(12).ToString.Trim '  subDs.Rows(x)("Ürün Kodu").ToString.trim.
                                _subCategory.UrunAdi = foundRows(x).Item(13).ToString.Trim ' subDs.Rows(x)("Ürün Adı").ToString.trim.
                                _subCategory.Fiyat = foundRows(x).Item(14).ToString.Trim ' subDs.Rows(x)("Fiyat").ToString.trim.
                                _category.DeepCategories.Add(_subCategory)
                            Next x
                        End If

                        _categorylist.Categorylist.Add(_category)
                    Next

                End If

                da.Dispose()
                ds.Dispose()
                subDs.Dispose()

                'Dim empty As String = String.Empty
                empty = JsonConvert.SerializeObject(_categorylist, Newtonsoft.Json.Formatting.Indented)

                'context.Response.Write(empty) if u use direct post to web page 
            End Using

        Catch ex As Exception
            ' context.Response.Write(ex.Message)
        End Try

    `