【发布时间】:2014-04-14 14:21:38
【问题描述】:
在过去的几天里,这一直让我发疯。我正在连接到 Shopify API 并下载 JSON 数据。我已经能够使用下面的类和代码成功地将数据反序列化为对象。但是,对象“customer”返回的属性(根据类)等于“Nothing”。到底是怎么回事?任何帮助将不胜感激!!附:我应该提到我正在使用 Newtonsoft JSON.NET。
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)
可以在http://docs.shopify.com/api/order 看到 JSON 数据的示例。
【问题讨论】:
-
尝试:Dim customer As New IEnumerable(Of ParsedJSON.Customer2) = JsonConvert.DeserializeObject(Of ParsedJSON.Customer2)(json_data)
-
感谢您的回复,但它返回此错误:
An unhandled exception of type 'System.InvalidCastException' occurred in ShopifyOrderManager.exe Additional information: Unable to cast object of type 'Customer2' to type 'System.Collections.Generic.IEnumerable1[ShopifyOrderManager.ParsedJSON+Customer2]。`
标签: json vb.net json.net deserialization shopify