RandomlyOnside
RandomlyOnside

Reputation: 445

Deseralization of JSON to VB.NET object getting NULL

I have a JSON string that i'm trying to deserialize in a VB.NET object but i'm getting nothing but nothing/nulls. Below is the VB.net code i'm doing the deserlization. The object, and the string version of what i'm sending to the deserialization, and what i'm getting back. I should be getting back what i'm passing. Here is some info from Chrome console that might be helpful. http://screencast.com/t/qBaXhvAS. The red are what i'm passing, the yellows are what i'm getting back.

Thanks!

VB.NET

    Dim serializer As New JavaScriptSerializer()
    Dim response As IMSClassLibrary.PhotoModelManagerCL.objOutfitModel = serializer.Deserialize(Of IMSClassLibrary.PhotoModelManagerCL.objOutfitModel)(JSON)
return response 

VB.NET Object

<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 what i'm passing and what i should be getting back once 'deserialized'

{"_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"}

what i'm actually getting back

{"_AddressLine1":null,"_AddressLine2":null,"_City":null,"_Girth":null,"_M3SizeCode":null,"_ModelFirstName":null,"_ModelLastName":null,"_ModelNotes":null,"_OutfitModelKey":null,"_ShoeSize":null,"_State":null,"_Zip":null}

Upvotes: 0

Views: 920

Answers (1)

Mark
Mark

Reputation: 8160

Your JSON has your field names prefixed with underscores, e.g. _AddressLine1, which corresponds to the member variable names in your class. However, these are Private, but the JavaScriptSerializer will only use Public member variables or properties. So, you can either change your JSON to drop the underscore prefix (in which case the Public properties will be used) or make the member variables Public.

There may be a way to override this with JavaScriptSerializer. Also, it looks like if you switched to JSON.net things make be more flexible.

Upvotes: 1

Related Questions