【问题标题】:I am trying to deserialize a JSON string using VB.NET 2.0我正在尝试使用 VB.NET 2.0 反序列化 JSON 字符串
【发布时间】:2015-06-18 01:53:51
【问题描述】:

我正在尝试反序列化一个 JSON 字符串并且卡住了。

我的字符串是

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

我已将它保存在字符串 embels 中

我的课是

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

结束类

我尝试反序列化使用

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

但是得到错误 Value of type 'Embellishments' cannot be convert to 'System.Collections.Generic.List(Of Embellishments)'

我现在卡住了。任何人都可以给我任何指示吗? 谢谢

编辑

感谢@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

但我收到错误“调用的目标已引发异常”

【问题讨论】:

  • 该代码对我来说工作正常 - 我的版本和你的版本都适用。 VB NET 2.0 是什么意思?您必须使用 NET Framework 3.5 或更高版本,因为那是引入 JavaScriptSerializer 的时候。等等——你的课搞砸了。
  • 我已将所有私有变量声明为整数?

标签: json vb.net deserialization


【解决方案1】:

问题是这样的:

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

您告诉Deserializer 它正在作用于Embellishments 对象,但试图在collection 对象中捕获结果。错误消息告诉您,如果您说字符串是单个对象,则它不能反序列化为它们的列表。换句话说,Embellishments 与List(of Embellishments) 不同。

由于字符串中有多个对象,因此正确的语法是(我叫我的Basket):

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

在 VS2010 及更高版本中,您可以使用 Option Infer 来分配类型。这也是有效的,并将创建一个篮子项目数组:

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

更重要的是您应该打开 Option Strict。这导致废话:

Private _ProductEmbellishmentID As Integer
Public Property ProductEmbellishmentID() As String

支持字段类型与属性 Getter/Setter 类型不匹配,将导致混乱。更改 Property 语句以匹配私有支持字段的类型。同样,在 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

【讨论】:

  • 嗨,谢谢。说得通。但是当我尝试它时出现错误。这里看不到如何添加代码,所以我将在下面添加它
  • 非常感谢。是的,您的答案是正确的,这是导致错误的类型不匹配。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 2013-01-18
  • 2013-05-07
  • 2018-04-28
  • 1970-01-01
相关资源
最近更新 更多