2014-04-14 12 views
0

過去幾天這讓我瘋狂。我連接到Shopify API並下載JSON數據。我已經能夠使用下面的類和代碼成功將數據反序列化爲一個對象。然而,對象「客戶」回來的屬性(按類)等於「無」。到底是怎麼回事?任何幫助將不勝感激!!附:我應該提到我正在使用Newtonsoft JSON.NET。反序列化的JSON數據在VB.NET中返回「Nothing」的屬性

Partial Public Class ParsedJSON 
    Public Class Customer2 
     Public Property accepts_marketing As Boolean 
     Public Property created_at As DateTime 
     Public Property email As String 
     Public Property first_name As String 
     Public Property id As Integer 
     Public Property last_name As String 
     Public Property last_order_id As Integer 
     Public Property multipass_identifier As Object 
     Public Property note As Object 
     Public Property orders_count As Integer 
     Public Property state As String 
     Public Property total_spent As String 
     Public Property updated_at As DateTime 
     Public Property verified_email As Boolean 
     Public Property tags As String 
     Public Property last_order_name As String 
     Public Property default_address As DefaultAddress 
    End Class 
End Class 

Partial Public Class ParsedJSON 
    Public Class Order 
     Public Property buyer_accepts_marketing As Boolean 
     Public Property cancel_reason As Object 
     Public Property cancelled_at As Object 
     Public Property cart_token As String 
     Public Property checkout_token As String 
     Public Property closed_at As Object 
     Public Property confirmed As Boolean 
     Public Property created_at As DateTime 
     Public Property currency As String 
     Public Property email As String 
     Public Property financial_status As String 
     Public Property fulfillment_status As Object 
     Public Property gateway As String 
     Public Property id As Integer 
     Public Property landing_site As String 
     Public Property location_id As Object 
     Public Property name As String 
     Public Property note As String 
     Public Property number As Integer 
     Public Property reference As Object 
     Public Property referring_site As String 
     Public Property source As String 
     Public Property source_identifier As Object 
     Public Property source_name As String 
     Public Property source_url As Object 
     Public Property subtotal_price As String 
     Public Property taxes_included As Boolean 
     Public Property test As Boolean 
     Public Property token As String 
     Public Property total_discounts As String 
     Public Property total_line_items_price As String 
     Public Property total_price As String 
     Public Property total_price_usd As String 
     Public Property total_tax As String 
     Public Property total_weight As Integer 
     Public Property updated_at As DateTime 
     Public Property user_id As Object 
     Public Property browser_ip As Object 
     Public Property landing_site_ref As Object 
     Public Property order_number As Integer 
     Public Property discount_codes As Object() 
     Public Property note_attributes As Object() 
     Public Property processing_method As String 
     Public Property checkout_id As Integer 
     Public Property tax_lines As TaxLine() 
     Public Property tags As String 
     Public Property line_items As LineItem() 
     Public Property shipping_lines As ShippingLine() 
     Public Property billing_address As BillingAddress 
     Public Property shipping_address As ShippingAddress 
     Public Property fulfillments As Object() 
     Public Property client_details As ClientDetails 
     Public Property customer As Customer2 
    End Class 
End Class 

Partial Public Class ParsedJSON 
    Public Property orders As Order() 
End Class 

... You get the idea 

現在,這裏是我的實現代碼:

 ' Calls the method GetResponseStream to return the stream associated with the response. 

     Dim receiveStream As Stream = response.GetResponseStream() 
     Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8") 

     ' Pipes the response stream to a higher level stream reader with the required encoding format. 

     Dim readStream As New StreamReader(receiveStream, encode) 
     Dim json_data As String = readStream.ReadToEnd() 

     Dim serializer As New JavaScriptSerializer() 
     Dim customer As ParsedJSON.Customer2 = JsonConvert.DeserializeObject(Of ParsedJSON.Customer2)(json_data) 

JSON數據可以在http://docs.shopify.com/api/order可以看到的一個例子。

+0

嘗試:昏暗的客戶作爲新的IEnumerable(的ParsedJSON.Customer2)= JsonConvert.DeserializeObject(的ParsedJSON.Customer2)(json_data) – user3036342

+0

謝謝你的迴應,但它返回了這個錯誤:'未處理的類型'系統異常。 ShopifyOrderManager.exe中發生InvalidCastException 附加信息:無法轉換'Customer2'類型的對象以鍵入'System.Collections.Generic.IEnumerable'1 [ShopifyOrderManager.ParsedJSON + Customer2]。' – user3519609

回答

0

防守性編碼。 Shopify可以通過API提供空訂單記錄,這是一個衆所周知的問題。如果遇到其中之一,請提供一些安全的默認設置。 Webhooks也可以接收空客戶的訂單。

而當你解決你的問題,與有效的客戶燒傷,一切都會對你有好處。

+0

但我知道JSON字符串有有效的數據。它只是當我試圖反序列化,它是給空值.. – user3519609