简体   繁体   中英

I am trying to deserialize a JSON string using VB.NET 2.0

I am trying to deserialize a JSON string and am stuck.

My string is

[{"BasketItemID":3,"ProductEmbellishmentID":8,"EmbellishmentText":"lfo","Price":9.95},{"BasketItemID":3,"ProductEmbellishmentID":3,"EmbellishmentText":"rc","Price":9.95}]

I have saved this in the string embels

My class is

Public Class Embellishments
Private _BasketItemID As Integer
Private _ProductEmbellishmentID As Integer
Private _EmbellishmentText As Integer
Private _Price As Integer


Public Property BasketItemID() As Integer
    Get
        Return _BasketItemID
    End Get

    Set(value As Integer)
        _BasketItemID = value
    End Set
End Property
Public Property ProductEmbellishmentID() As String
    Get
        Return _ProductEmbellishmentID
    End Get
    Set(value As String)
        _ProductEmbellishmentID = value
    End Set
End Property
Public Property EmbellishmentText() As String
    Get
        Return _EmbellishmentText
    End Get
    Set(value As String)
        _EmbellishmentText = value
    End Set
End Property
Public Property Price() As Decimal
    Get
        Return _Price
    End Get
    Set(value As Decimal)
        _Price = value
    End Set
End Property

End Class

And I try to deserialize using

        Dim jss As New JavaScriptSerializer()
        Dim emblist As List(Of Embellishments) = jss.Deserialize(Of Embellishments)(embels)

But get the error Value of type 'Embellishments' cannot be converted to 'System.Collections.Generic.List(Of Embellishments)'

I am now stuck. Can anyone give me any pointers? thanks

EDIT

Thanks to @Plutonix I have now tried

    Dim errormessage As String=''
Dim Count As Integer = 0
Try
    Dim jss As New JavaScriptSerializer()
    Dim emblist As List(Of Embellishments) = jss.Deserialize(Of List(Of Embellishments))(embels)

    For Each em As Embellishments In emblist
        Count = Count + 1
    Next

Catch ex As Exception

    errormessage = ex.Message

End Try

BUt I get the error 'Exception has been thrown by the target of an invocation'

The problem is this:

Dim emblist As List(Of Embellishments) = jss.Deserialize(Of Embellishments)(embels)

You are telling the Deserializer it is acting on an Embellishments object, but trying to catch the result in a collection object. The error message is telling you that if you are saying that the string is a single object, it cant deserialize to a List of them. In other words, Embellishments is not the same thing as a List(of Embellishments) .

Since there is more than one object in the string, the correct syntax is (I called mine Basket ):

Dim emblist = jss.Deserialize(Of List(Of Basket))(embels)

In VS2010 and beyond, you can use Option Infer to assign the type. This is also valid and will create an array of Basket Items:

Dim emblist = jss.Deserialize(Of Basket())(embels)

More Importantly you should turn on Option Strict. This results in nonsense:

Private _ProductEmbellishmentID As Integer
Public Property ProductEmbellishmentID() As String

The backing field Type doesnt match the property Getter/Setter type and will result in mayhem. Change the Property statements to match the type of the private backing fields. Again, after VS2010 you can reduce all that boilerplate code with autoimplement properties:

Public Class Basket
    Public Property BasketItemID As Integer
    Public Property ProductEmbellishmentID As Integer
    Public Property EmbellishmentText As String
    Public Property Price As Decimal

End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM