简体   繁体   English

我正在尝试使用VB.NET 2.0反序列化JSON字符串

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

I am trying to deserialize a JSON string and am stuck. 我正在尝试反序列化JSON字符串并卡住。

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)' 但是出现错误“类型”值的值不能转换为“ 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 感谢@Plutonix我现在尝试了

    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. 您要告诉Deserializer它正在对Embellishments对象起作用,但试图将结果捕获到集合对象中。 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) . 换句话说, EmbellishmentsList(of Embellishments)是不同的东西。

Since there is more than one object in the string, the correct syntax is (I called mine Basket ): 由于在字符串中的多个对象,正确的语法是(我叫我的Basket ):

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

In VS2010 and beyond, you can use Option Infer to assign the type. 在VS2010及更高版本中,可以使用Option Infer分配类型。 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. 更重要的是,您应该启用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. 支持字段Type与属性Getter / Setter type不匹配,将导致混乱。 Change the Property statements to match the type of the private backing fields. 更改Property语句以匹配专用后备字段的类型。 Again, after VS2010 you can reduce all that boilerplate code with autoimplement properties: 同样,在VS2010之后,您可以使用自动实现属性来减少所有样板代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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