我有一个JSON字符串,我试图在VB.NET对象中反序列化,但我什么都没有,但没有什么/ nulls。下面是我正在进行deserlization的VB.net代码。对象,以及我发送到反序列化的字符串版本,以及我要回来的内容。我应该回到我正在经历的事情。以下是Chrome控制台中可能有用的一些信息。 http://screencast.com/t/qBaXhvAS。红色是我经过的,黄色是我要回来的。
谢谢!
VB.NET
Dim serializer As New JavaScriptSerializer()
Dim response As IMSClassLibrary.PhotoModelManagerCL.objOutfitModel = serializer.Deserialize(Of IMSClassLibrary.PhotoModelManagerCL.objOutfitModel)(JSON)
return response
VB.NET对象
<Serializable()> Public Class objOutfitModel
Private _OutfitModelKey As String
Public Property OutfitModelKey() As String
Get
Return _OutfitModelKey
End Get
Set(ByVal value As String)
_OutfitModelKey = value
End Set
End Property
Private _ModelFirstName As String
Public Property ModelFirstName() As String
Get
Return _ModelFirstName
End Get
Set(ByVal value As String)
_ModelFirstName = value
End Set
End Property
Private _ModelLastName As String
Public Property ModelLastName() As String
Get
Return _ModelLastName
End Get
Set(ByVal value As String)
_ModelLastName = value
End Set
End Property
Private _M3SizeCode As String
Public Property M3SizeCode() As String
Get
Return _M3SizeCode
End Get
Set(ByVal value As String)
_M3SizeCode = value
End Set
End Property
Private _Girth As String
Public Property Girth() As String
Get
Return _Girth
End Get
Set(ByVal value As String)
_Girth = value
End Set
End Property
Private _ShoeSize As String
Public Property ShoeSize() As String
Get
Return _ShoeSize
End Get
Set(ByVal value As String)
_ShoeSize = value
End Set
End Property
Private _AddressLine1 As String
Public Property AddressLine1() As String
Get
Return _AddressLine1
End Get
Set(ByVal value As String)
_AddressLine1 = value
End Set
End Property
Private _AddressLine2 As String
Public Property AddressLine2() As String
Get
Return _AddressLine2
End Get
Set(ByVal value As String)
_AddressLine2 = value
End Set
End Property
Private _City As String
Public Property City() As String
Get
Return _City
End Get
Set(ByVal value As String)
_City = value
End Set
End Property
Private _State As String
Public Property State() As String
Get
Return _State
End Get
Set(ByVal value As String)
_State = value
End Set
End Property
Private _Zip As String
Public Property Zip() As String
Get
Return _Zip
End Get
Set(ByVal value As String)
_Zip = value
End Set
End Property
Private _ModelNotes As String
Public Property ModelNotes() As String
Get
Return _ModelNotes
End Get
Set(ByVal value As String)
_ModelNotes = value
End Set
End Property
End Class
JSON 我通过了什么以及一旦'反序列化'我应该得到什么
{"_AddressLine1":"123 arsenal","_AddressLine2":"apt 1","_City":"saint louis","_Girth":"14","_M3SizeCode":"71","_ModelFirstName":"joshua","_ModelLastName":"harris","_ModelNotes":"testing 123","_OutfitModelKey":0,"_ShoeSize":"20","_State":"ME","_Zip":"63031"}
我实际上回来了
{"_AddressLine1":null,"_AddressLine2":null,"_City":null,"_Girth":null,"_M3SizeCode":null,"_ModelFirstName":null,"_ModelLastName":null,"_ModelNotes":null,"_OutfitModelKey":null,"_ShoeSize":null,"_State":null,"_Zip":null}
答案 0 :(得分:1)
您的JSON的字段名称前缀为下划线,例如_AddressLine1
,对应于类中的成员变量名称。但是,这些是Private
,但JavaScriptSerializer
只会使用Public
成员变量或属性。因此,您可以更改JSON以删除下划线前缀(在这种情况下将使用Public
属性)或创建成员变量Public
。
可能有一种方法可以用JavaScriptSerializer
覆盖它。此外,如果您切换到JSON.net,则看起来好像是more flexible。