使用JSON.Net进行反序列化为所有字段返回null(VB)

时间:2015-10-15 19:15:38

标签: json vb.net serialization json.net restsharp

我无法弄清楚为什么所有字段都返回null。我的课程看起来很正确,非常适合json。我使用JSON Util工具将我的JSON结果转换为VB(http://jsonutils.com/)。我在C#中尝试了这个项目,但最终导致了同样的问题。我正在使用RestSharp来发出请求,而JSON.NET则反序列化我的对象。

请注意,RestSharp内容不为空,等于我在下面添加的JSON。

这是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}}}

这里我的班级都曾用于反序列化:

    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

这是反序列化函数:

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

这里我调用我的函数来检索我的对象:

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

1 个答案:

答案 0 :(得分:2)

看看JSON的开头:

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

创建了一个外部容器来容纳&#34;客户&#34;你的代码没有考虑到的。如果您不想为它创建一个do-nothing类,请先解析结果:

Dim jstr = ...from whereever

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

使用课程:

Public Class CustContainer
    Public Property customer As Customer
End Class

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

我不喜欢第二个,因为它要求所有其余的引用都是cust.customer.Foo,所以我更愿意丢弃它们。