Pilouk
Pilouk

Reputation: 1317

Deserialization using JSON.Net return null for all fields (VB)

I can't figure it out why ALL fields return null. My classes seem correct and fit the json perfectly. I used the JSON Util tool to convert my JSON result to VB (http://jsonutils.com/). I tried the project in C# but end up resulting the same problem. I'm using RestSharp to make my request and JSON.NET to deserialize my object.

Note that the RestSharp content is not empty and is equal to the JSON i added below.

Here is the Json :

{"customer":{"created_at":"2015-10-10T16:35:07-04:00","cust_identifier":null,"description":null,"domains":null,"id":8000039822,"name":"ACTION VEHICULES D'OCCASION","note":null,"sla_policy_id":8000012148,"updated_at":"2015-10-15T09:43:09-04:00","custom_field":{"cf_etat_client":"NO","cf_dealer_code":"U4504033884","cf_manufacturer":"Used","cf_email":null,"cf_province":"QU\u00c9BEC","cf_region":null,"cf_address":"1913 CH CHAMBLY","cf_city":"CARIGNAN","cf_postal_code":null,"cf_phone_":"450 403-3884","cf_fax":null,"cf_tollfree":null,"cf_legal_name":"ACTION VEHICULES D'OCCASION","cf_dealer_princ":null,"cf_g_manager":null,"cf_cfo_finance":null,"cf_sales_manager":null,"cf_trade_resp":null,"cf_used_veh_manager":null,"cf_business_manager_1":null,"cf_business_manager_2":null,"cf_email_g_manager":null,"cf_email_gs_manager":null,"cf_email_s_manager":null,"cf_email_trade":null,"cf_fleet_lease":null,"cf_accounting":null,"cf_installed":null,"cf_demo":null,"cf_update":null,"cf_sold":null,"cf_dealer_website":null,"cf_i_t_contact":null,"cf_server_name":null,"cf_network":null,"cf_is_domain":null,"cf_easybackup":null,"cf_password":null,"cf_pc_installed":null,"cf_reference_by":null,"cf_error_":null,"cf_license_":null,"cf_is_amvoq":true}}}

Here both of my class i used to deserialize :

    Public Class Customer
    Public Property created_at As DateTime
    Public Property cust_identifier As Object
    Public Property description As Object
    Public Property domains As Object
    Public Property id As Long
    Public Property name As String
    Public Property note As Object
    Public Property sla_policy_id As Long
    Public Property updated_at As DateTime
    Public Property custom_field As CustomField
End Class


Public Class CustomField
    Public Property cf_etat_client As String
    Public Property cf_dealer_code As String
    Public Property cf_manufacturer As String
    Public Property cf_email As Object
    Public Property cf_province As String
    Public Property cf_region As Object
    Public Property cf_address As String
    Public Property cf_city As String
    Public Property cf_postal_code As Object
    Public Property cf_phone_ As String
    Public Property cf_fax As Object
    Public Property cf_tollfree As Object
    Public Property cf_legal_name As String
    Public Property cf_dealer_princ As Object
    Public Property cf_g_manager As Object
    Public Property cf_cfo_finance As Object
    Public Property cf_sales_manager As Object
    Public Property cf_trade_resp As Object
    Public Property cf_used_veh_manager As Object
    Public Property cf_business_manager_1 As Object
    Public Property cf_business_manager_2 As Object
    Public Property cf_email_g_manager As Object
    Public Property cf_email_gs_manager As Object
    Public Property cf_email_s_manager As Object
    Public Property cf_email_trade As Object
    Public Property cf_fleet_lease As Object
    Public Property cf_accounting As Object
    Public Property cf_installed As Object
    Public Property cf_demo As Object
    Public Property cf_update As Object
    Public Property cf_sold As Object
    Public Property cf_dealer_website As Object
    Public Property cf_i_t_contact As Object
    Public Property cf_server_name As Object
    Public Property cf_network As Object
    Public Property cf_is_domain As Object
    Public Property cf_easybackup As Object
    Public Property cf_password As Object
    Public Property cf_pc_installed As Object
    Public Property cf_reference_by As Object
    Public Property cf_error_ As Object
    Public Property cf_license_ As Object
    Public Property cf_is_amvoq As Boolean
End Class

Here is the Deserialize Function :

Public Shared Function JSONDeserializeFreshDeskCie(repContent As Stream) As Customer
    Dim rs As Customer = Nothing
    Dim test As Object
    Dim serializer As New JsonSerializer()
    Try


        Using sr As New StreamReader(repContent)
            Using jsonTextReader As New JsonTextReader(sr)

                rs = serializer.Deserialize(Of Customer)(jsonTextReader)

            End Using


        End Using

    Catch ex As Exception
        Throw New Exception(ex.Message, ex)
    End Try


    Return rs

End Function

Here where i call my function to retrieve my object :

Private Sub loadAllCie(ByVal e As IRestResponse)
Dim rep As Stream = Nothing

Dim rs As Customer
Try

    rep = New MemoryStream(e.RawBytes())

    If e.ErrorException IsNot Nothing OrElse e.StatusCode <> Net.HttpStatusCode.OK Then

        Dim strError As String = ""

        If e.ErrorException IsNot Nothing Then
            strError = "Error : " & e.ErrorException.Message & vbCrLf
        End If

        strError &= "Web Error : " & e.ErrorMessage

        strError &= vbCrLf & e.StatusCode.ToString()
        MessageBox.Show(strError)
        Exit Try
    End If


    rs = JSONSerialization.JSONDeserializeFreshDeskCie(rep)

    Dim allo As String = ""

Catch ex As Exception
    MessageBox.Show(ex.Message)
Finally
    If rep IsNot Nothing Then
        rep.Close()
    End If
End Try


End Sub

Upvotes: 1

Views: 1800

Answers (1)

Look at the start of the JSON:

{"customer":{"created_at"...

There is an outer container created to hold "customer" which your code does not account for. If you do not want to create a do-nothing class for it, parse the result first:

Dim jstr = ...from whereever

Dim jobj = JObject.Parse(jstr)
Dim cust = JsonConvert.DeserializeObject(Of Customer)(jobj("customer").ToString)

To use a class:

Public Class CustContainer
    Public Property customer As Customer
End Class

...
Dim cust = JsonConvert.DeserializeObject(Of CustContainer)(jstr)

I dont like the second because it requires all the rest of the references to be cust.customer.Foo, so I prefer to discard them.

Upvotes: 2

Related Questions