反序列化的JSON数据在VB.NET中返回“Nothing”的属性

时间:2014-04-14 14:21:39

标签: json vb.net json.net deserialization shopify

过去几天这让我很生气。我正在连接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数据的示例。

1 个答案:

答案 0 :(得分:0)

防守代码。 Shopify可以通过具有空客户记录的API提供订单,这是一个众所周知的问题。如果遇到其中一个,请提供一些安全的默认值。 Webhooks也可以通过null客户接收订单。

当你解决与有效客户混淆的问题时,一切都会好起来的。