对所有字段 (VB) 使用 JSON.Net return null 进行反序列化

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

我不明白为什么所有字段 return 都为空。我的 classes 看起来是正确的并且非常适合 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}}}

这是我用来反序列化的 class :

    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

查看 JSON 的开头:

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

创建了一个外部容器来容纳 "customer",您的代码没有考虑到这一点。如果你不想为它创建一个什么都不做的class,先解析结果:

Dim jstr = ...from whereever

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

要使用 class:

Public Class CustContainer
    Public Property customer As Customer
End Class

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

我不喜欢第二个,因为它要求所有其他参考文献都是 cust.customer.Foo,所以我宁愿放弃它们。