Json将对象反序列化为空

时间:2013-11-14 10:38:53

标签: json vb.net vb.net-2010

我有以下函数来反序列化json:

 Public Sub getDataTable(ByVal json As String)

    json = <json>{"TYPE_OF_HANDSET": "4","SMS": [{"DATE": "19.10.2013 13:20","FROM": "SAWA Promo","MESSAGE": "Special offer only for you! Get 300 MB internet + 200 SR Extra Bonus valid for 7 days for only 20 SR. Send 1 to 804097 via SMS. Extra bonus can be used to call and SMS all STC numbers. Happy browsing with SAWA!"},{"DATE": "19.10.2013 13:20","FROM": "SAWA Promo","MESSAGE": "Special offer only for you! Get 300 MB internet + 200 SR Extra Bonus valid for 7 days for only 20 SR. Send 1 to 804097 via SMS. Extra bonus can be used to call and SMS all STC numbers. Happy browsing with SAWA!"}],"DESC": "Samsung GT-I9100","ID_USER": "261"}</json>.Value

    Dim data As New DataBck
    data = JsonConvert.DeserializeObject(Of DataBck)(json)
    handsetType = data.handset
    DESC = data.DESC
    ID_USER = data.ID_USER
    smsDT = data.sms
End Sub

以及以下课程:

Public Class DataBck
<System.Runtime.Serialization.DataMember(Name:="TYPE_OF_HANDSET")>
Public handset As Integer
<System.Runtime.Serialization.DataMember(Name:="DESC")>
Public DESC As String
<System.Runtime.Serialization.DataMember(Name:="ID_USER")>
Public ID_USER As Integer
Public sms As DataTable
End Class

当我反序列化json时,所有字段名称都被正确填充,只有字段名称TYPE_OF_HANDSET被填充为空,当我用HANDSET替换它时它被正确填充虽然ID_USER正确填充而没有删除下划线...

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我假设你在样本json中使用'Handset'。看起来反序列化不会查看您的名称属性。您会注意到您目前在属性名称上获得匹配。这就是Handset和user_id匹配的原因。如果你使用

public Type_Of_Handset as Integer

它会起作用。