2015-10-15 48 views
1

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

请注意,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 

回答

2

看那JSON的开始:

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

有创造举办“客户”你的代码不考虑外部容器。如果你不想为它创建一个什么都不做一流,分析结果:因为它需要所有的休息

Public Class CustContainer 
    Public Property customer As Customer 
End Class 

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

我不喜欢第二:

Dim jstr = ...from whereever 

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

使用类的参考文献是cust.customer.Foo,所以我宁愿放弃它们。

+0

感谢上帝......疯了,不知道我需要容器..现在我明白了为什么它是在JSON UTILS工具上。谢谢 – Pilouk

+0

外部容器用于存放识别被反序列化的类型所需的“客户”标识符。 – Plutonix