来自 Cratejoy 的嵌套 JSON 的反序列化

托马斯·J。

我正在尝试从 Cratejoy 反序列化以下 JSON,并将shipping_id 列表获取到 DataGridView 中,但它似乎不适合我。

JSON 看起来像这样:

{
  "count": 2,
  "next": null,
  "prev": null,
  "results": [
    {
      "adjusted_ordered_at": "2019-08-14T14:01:40Z",
      "created_at": "2019-08-14T14:01:40Z",
      "customer_id": 398862090,
      "feedback_responses": [],
      "fulfillments": [
        {
          "adjusted_fulfillment_date": "2019-08-14T14:01:40Z",
          "cycle_number": 0,
          "fulfillment_date": "2019-08-14T14:01:40Z",
          "id": 2349318760,
          "instance": {
            "deleted": false,
            "gift": false,
            "id": 1445951944,
            "images": [],
            "inventory": null,
            "price": 0,
            "product": {
              "deleted": false,
              "id": 1445951937,
              "mp_visible": true,
              "name": "Monthly CULTURE Box ",
              "single_purchasable": false
            },
            "product_id": 1445951937,
            "ship_weight": 10.0,
            "sku": "SQ6217913",
            "term_prices": [
              {
                "id": 1445951946,
                "instance_id": 1445951944,
                "price": 1699,
                "term_id": 1445951938,
                "type": "product_instance_subscription_type_term_price"
              },
              {
                "id": 1445951951,
                "instance_id": 1445951944,
                "price": 4899,
                "term_id": 1445951939,
                "type": "product_instance_subscription_type_term_price"
              },
              {
                "id": 1445951952,
                "instance_id": 1445951944,
                "price": 9599,
                "term_id": 1445951940,
                "type": "product_instance_subscription_type_term_price"
              },
              {
                "id": 1445951953,
                "instance_id": 1445951944,
                "price": 18299,
                "term_id": 1445951941,
                "type": "product_instance_subscription_type_term_price"
              }
            ],
            "type": "product_instance",
            "variants": []
          },
          "is_test": false,
          "order": {
            "card_refunded_amount": 0,
            "credit_applied": 0,
            "customer": {
              "country": "GB",
              "email": "[email protected]",
              "first_name": "Sarah",
              "id": 398862090,
              "last_name": "Chug",
              "location": "GB",
              "name": "Sarah Chug",
              "type": "customer"
            },
            "customer_id": 398862090,
            "financial_status": "paid",
            "fulfillment_status": "fulfilled",
            "gift_card_discount": 0,
            "gift_message": null,
            "gift_renewal_notif": false,
            "gross_shipping": 0,
            "id": 2349292405,
            "is_gift": false,
            "is_renewal": false,
            "is_test": false,
            "note": null,
            "order_gift_info": null,
            "placed_at": "2019-08-14T13:39:49Z",
            "prorated_charge": null,
            "received_gift": null,
            "refund_applied": null,
            "refunded_amount": 0,
            "sent_gift": null,
            "status": "closed",
            "store_id": 147108660,
            "sub_total": 1699,
            "subscriptions": [
              {
                "autorenew": true,
                "billing": {
                  "id": 1445951935,
                  "rebill_day": 15,
                  "rebill_months": 1,
                  "rebill_weeks": null,
                  "rebill_window": 10,
                  "store_id": 147108660,
                  "type": "product_billing"
                },
                "billing_name": "Monthly",
                "credit": null,
                "customer": {
                  "country": "GB",
                  "email": "[email protected]",
                  "first_name": "Sarah",
                  "id": 398862090,
                  "last_name": "Chug",
                  "location": "GB",
                  "name": "Sarah Chug",
                  "type": "customer"
                },
                "end_date": "2019-09-15T00:00:00Z",
                "id": 2349318754,
                "is_test": false,
                "merchant_billing": null,
                "note": null,
                "product_billing_id": 1445951935,
                "skipped_date": null,
                "source": 3,
                "start_date": "2019-08-14T14:01:40Z",
                "status": "active",
                "store_id": 147108660,
                "term": {
                  "description": "Charged every month",
                  "enabled": true,
                  "id": 1445951938,
                  "images": [],
                  "name": "Month to Month",
                  "num_cycles": 1,
                  "type": "subscription_type_term"
                },
                "type": "subscription"
              }
            ],
            "total": 1699,
            "total_app_fees": null,
            "total_label_cost": null,
            "total_pending_fees": null,
            "total_price": 1699,
            "total_shipping": 0,
            "total_tax": 0,
            "transaction_fee": 30,
            "transaction_fee_status": 1,
            "type": "order"
          },
          "shipment_id": 2349318762,
          "shipping": 0,
          "status": "open",
          "subscription_id": 2349318754,
          "tax": 0,
          "test": false,
          "total_cycles": 1,
          "type": "fulfillment"
        }
      ],
      "id": 2349318762,
      "is_gift": false,
      "is_test": false,
      "labels": [],
      "ship_address": {
        "city": "london",
        "company": "",
        "country": "GB",
        "icon": "United-Kingdom.png",
        "id": 2349292377,
        "phone_number": "7835888303",
        "state": "",
        "status": 3,
        "status_message": "Cannot verify international addresses.",
        "street": "6 victoria gardens, heston",
        "to": "ALICE Kingdom",
        "type": "address",
        "unit": "heston",
        "zip_code": "TW59DE"
      },
      "shipped_at": null,
      "status": "unshipped",
      "target_at": "2019-08-14T14:01:40Z",
      "tracking_number": null,
      "type": "shipment",
      "url": "/v1/shipments/2349318762/"
    },
    {
      "adjusted_ordered_at": "2019-08-14T15:39:32Z",
      "created_at": "2019-08-14T15:39:32Z",
      "customer_id": 2349459101,
      "feedback_responses": [],
      "fulfillments": [
        {
          "adjusted_fulfillment_date": "2019-08-14T15:39:32Z",
          "cycle_number": null,
          "fulfillment_date": "2019-08-14T15:39:32Z",
          "id": 2349459176,
          "instance": {
            "deleted": false,
            "gift": false,
            "id": 2347700148,
            "images": [],
            "inventory": {
              "confidence": 10,
              "id": 2347700153,
              "out_of_stock_purchases": false,
              "product_instance_id": 2347700148,
              "quantity_on_hand": 13,
              "track_inventory": true,
              "type": "product_inventory"
            },
            "price": 1499,
            "product": {
              "deleted": false,
              "id": 2347700147,
              "mp_visible": true,
              "name": "Inspire Me Korea TRAVEL GUIDE BUNDLE [SAVE 17%]",
              "single_purchasable": true
            },
            "product_id": 2347700147,
            "ship_weight": 0.0,
            "sku": "SQ7195254",
            "term_prices": [],
            "type": "product_instance",
            "variants": []
          },
          "is_test": false,
          "order": {
            "card_refunded_amount": 0,
            "credit_applied": 0,
            "customer": {
              "country": "FR",
              "email": "[email protected]",
              "first_name": "Margaret",
              "id": 2349459101,
              "last_name": "Tatcher",
              "location": "FR",
              "name": "Margaret Tatcher",
              "type": "customer"
            },
            "customer_id": 2349459101,
            "financial_status": "paid",
            "fulfillment_status": "unfilled",
            "gift_card_discount": 0,
            "gift_message": null,
            "gift_renewal_notif": false,
            "gross_shipping": 699,
            "id": 2349459127,
            "is_gift": false,
            "is_renewal": false,
            "is_test": false,
            "note": null,
            "order_gift_info": null,
            "placed_at": "2019-08-14T15:39:28Z",
            "prorated_charge": null,
            "received_gift": null,
            "refund_applied": null,
            "refunded_amount": 0,
            "sent_gift": null,
            "status": "closed",
            "store_id": 147108660,
            "sub_total": 3897,
            "subscriptions": [
              {
                "autorenew": true,
                "billing": {
                  "id": 1445951935,
                  "rebill_day": 15,
                  "rebill_months": 1,
                  "rebill_weeks": null,
                  "rebill_window": 10,
                  "store_id": 147108660,
                  "type": "product_billing"
                },
                "billing_name": "Monthly",
                "credit": null,
                "customer": {
                  "country": "FR",
                  "email": "[email protected]",
                  "first_name": "Margeret",
                  "id": 2349459101,
                  "last_name": "Delaunay",
                  "location": "FR",
                  "name": "Margaret Tatcher",
                  "type": "customer"
                },
                "end_date": "2019-09-15T00:00:00Z",
                "id": 2349459172,
                "is_test": false,
                "merchant_billing": null,
                "note": null,
                "product_billing_id": 1445951935,
                "skipped_date": null,
                "source": 3,
                "start_date": "2019-08-14T15:39:32Z",
                "status": "active",
                "store_id": 147108660,
                "term": {
                  "description": "Charged every month",
                  "enabled": true,
                  "id": 1445951938,
                  "images": [],
                  "name": "Month to Month",
                  "num_cycles": 1,
                  "type": "subscription_type_term"
                },
                "type": "subscription"
              }
            ],
            "total": 3643,
            "total_app_fees": null,
            "total_label_cost": null,
            "total_pending_fees": null,
            "total_price": 2944,
            "total_shipping": 699,
            "total_tax": 0,
            "transaction_fee": 54,
            "transaction_fee_status": 1,
            "type": "order"
          },
          "shipment_id": 2349459174,
          "shipping": 0,
          "status": "open",
          "subscription_id": null,
          "tax": 0,
          "test": false,
          "total_cycles": null,
          "type": "fulfillment"
        },
        {
          "adjusted_fulfillment_date": "2019-08-14T15:39:32Z",
          "cycle_number": 0,
          "fulfillment_date": "2019-08-14T15:39:32Z",
          "id": 2349459177,
          "instance": {
            "deleted": false,
            "gift": false,
            "id": 1445951944,
            "images": [],
            "inventory": null,
            "price": 0,
            "product": {
              "deleted": false,
              "id": 1445951937,
              "mp_visible": true,
              "name": "Monthly CULTURE Box ",
              "single_purchasable": false
            },
            "product_id": 1445951937,
            "ship_weight": 10.0,
            "sku": "SQ6217913",
            "term_prices": [
              {
                "id": 1445951946,
                "instance_id": 1445951944,
                "price": 1699,
                "term_id": 1445951938,
                "type": "product_instance_subscription_type_term_price"
              },
              {
                "id": 1445951951,
                "instance_id": 1445951944,
                "price": 4899,
                "term_id": 1445951939,
                "type": "product_instance_subscription_type_term_price"
              },
              {
                "id": 1445951952,
                "instance_id": 1445951944,
                "price": 9599,
                "term_id": 1445951940,
                "type": "product_instance_subscription_type_term_price"
              },
              {
                "id": 1445951953,
                "instance_id": 1445951944,
                "price": 18299,
                "term_id": 1445951941,
                "type": "product_instance_subscription_type_term_price"
              }
            ],
            "type": "product_instance",
            "variants": []
          },
          "is_test": false,
          "order": {
            "card_refunded_amount": 0,
            "credit_applied": 0,
            "customer_id": 2349459102,
            "financial_status": "paid",
            "fulfillment_status": "unfilled",
            "gift_card_discount": 0,
            "gift_message": null,
            "gift_renewal_notif": false,
            "gross_shipping": 699,
            "id": 2349459123,
            "is_gift": false,
            "is_renewal": false,
            "is_test": false,
            "note": null,
            "placed_at": "2019-08-14T15:39:28Z",
            "prorated_charge": null,
            "refund_applied": null,
            "refunded_amount": 0,
            "status": "closed",
            "store_id": 147108660,
            "sub_total": 3897,
            "total": 3643,
            "total_app_fees": null,
            "total_label_cost": null,
            "total_pending_fees": null,
            "total_price": 2944,
            "total_shipping": 699,
            "total_tax": 0,
            "transaction_fee": 54,
            "transaction_fee_status": 1,
            "type": "order"
          },
          "shipment_id": 2349459174,
          "shipping": 699,
          "status": "open",
          "subscription_id": 2349459172,
          "tax": 0,
          "test": false,
          "total_cycles": 1,
          "type": "fulfillment"
        }
      ],
      "id": 2349459174,
      "is_gift": false,
      "is_test": false,
      "labels": [],
      "ship_address": {
        "city": "Chambray-lès-Tours",
        "company": "",
        "country": "FR",
        "icon": "France.png",
        "id": 2349459103,
        "phone_number": "06 58 71 19 33 ",
        "state": "",
        "status": 3,
        "status_message": "Cannot verify international addresses.",
        "street": "35 rue Vasco de Gama",
        "to": "John Smith",
        "type": "address",
        "unit": "",
        "zip_code": "37170"
      },
      "shipped_at": null,
      "status": "unshipped",
      "target_at": "2019-08-14T15:39:32Z",
      "tracking_number": null,
      "type": "shipment",
      "url": "/v1/shipments/2349459174/"
    }
  ]
}

我的代码如下所示:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim url As String = "https://api.cratejoy.com/v1/shipments/?with=fulfillments&created_at__gt=2019-08-01T10:00:00Z"

        'make a call
        Dim Request As HttpWebRequest = HttpWebRequest.Create(url)
        Request.Headers.Add("Authorization", "Basic logpassword")
        Request.Proxy = Nothing

        'get a response
        On Error Resume Next
        Dim Response As HttpWebResponse = Request.GetResponse
        Dim ResponseStream As System.IO.Stream = Response.GetResponseStream
        Dim StreamReader As New System.IO.StreamReader(ResponseStream)
        Dim jobj As String = StreamReader.ReadToEnd 'response
        Dim Status As String = Response.StatusCode
        Dim ContentLength As String = Response.ContentLength
        Dim StatusDescription As String = Response.StatusDescription
        MsgBox("Status: " & Status & vbNewLine +
               "Content Length: " & ContentLength & vbNewLine +
               "Status Description: " & StatusDescription & vbNewLine)
        If Status = 404 Or Status = 400 Then
            MsgBox("Error downloading data!")
            Exit Sub
        End If


        Label1.Text = jobj 'show me the JSON - works for smaller JSONs but doesn't want to show the big ones??

        'Deserialize
        Dim SObject As New List(Of Result)()
        SObject = JsonConvert.DeserializeObject(Of List(Of Result))(jobj)

        Dim a As Integer = 0

        For Each item As Object In SObject
            DataGridView1.Rows.Add(New String() {a + 1, item.shipment_id.ToString})
            a += 1
        Next

    End Sub

我的课程:

Public Class Root
    Public Property results As List(Of Result)()
End Class

Public Class Result
    Public Property adjusted_ordered_at As DateTime
    Public Property created_at As DateTime
    Public Property customer_id As Integer
    Public Property feedback_responses As Object()
    Public Property fulfillments As List(Of Fulfillment)()
    Public Property id As Long
    Public Property is_gift As Boolean
    Public Property is_test As Boolean
    Public Property labels As Object()
    Public Property ship_address As ShipAddress
    Public Property shipped_at As Object
    Public Property status As String
    Public Property target_at As DateTime
    Public Property tracking_number As Object
    Public Property type As String
    Public Property url As String
End Class

Public Class Fulfillment
    Public Property adjusted_fulfillment_date As DateTime
    Public Property cycle_number As Integer
    Public Property fulfillment_date As DateTime
    Public Property id As Long
    Public Property instance As Instance
    Public Property is_test As Boolean
    Public Property order As Order
    Public Property shipment_id As String
    Public Property shipping As Integer
    Public Property status As String
    Public Property subscription_id As Long
    Public Property tax As Integer
    Public Property test As Boolean
    Public Property total_cycles As Integer
    Public Property type As String
End Class

我猜 Classes 有问题,但我尝试了很多不同的方法,但无法弄清楚我做错了什么。

另外,有人知道我是否可以在 Cratejoy 上进行过滤以仅显示“未发货”的货件?我确定我可以,但是他们在http://docs.cratejoy.com/docs/filtering-and-searching2的说明不想使用地址:https : //api.cratejoy.com/v1/shipments /?fulfillments.status=open&created_at__gt=2019-08-14T14:00:00Z

另一件事 - 太大的 JSON 不想在 Label1.Text 上显示,我怎么能看到我的应用程序下载了什么?

感谢大家的帮助!

粗鲁

你也可以这样做:

Imports Newtonsoft.Json.Linq

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.Columns.Add("shipment_id", "Shipment ID")

        Dim ParseJSON As JObject = JObject.Parse(TextBox1.Text)

        For Each result As JToken In ParseJSON("results")
            For Each fullfilmentitem As JToken In result("fulfillments")
                DataGridView1.Rows.Add(fullfilmentitem("shipment_id"))
            Next
        Next
    End Sub
End Class

在此处输入图片说明


或者对于这两种状态:

Imports Newtonsoft.Json.Linq

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.Columns.Add("shipment_id", "Unshipped Shipment")
        DataGridView2.Columns.Add("shipment_id", "Shipped Shipment")

        Dim ParseJSON As JObject = JObject.Parse(TextBox1.Text)

        For Each result As JToken In ParseJSON("results")
            If result("status").ToString.Equals("unshipped") Then
                For Each fullfilmentitem As JToken In result("fulfillments")
                    DataGridView1.Rows.Add(fullfilmentitem("shipment_id"))
                Next
            Else 'shipped
                For Each fullfilmentitem As JToken In result("fulfillments")
                    DataGridView2.Rows.Add(fullfilmentitem("shipment_id"))
                Next
            End If
        Next
    End Sub
End Class

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从API反序列化嵌套的json

来自分类Dev

反序列化嵌套JSON

来自分类Dev

难以反序列化嵌套JSON

来自分类Dev

来自Json的Gson反序列化泛型

来自分类Dev

无法反序列化来自Flurry API的json响应

来自分类Dev

使用RestSharp反序列化嵌套的JSON数组

来自分类Dev

使用RestSharp Client反序列化嵌套的JSON响应

来自分类Dev

在C#中反序列化嵌套的JSON数组

来自分类Dev

GSON反序列化:嵌套JSon文件错误

来自分类Dev

Json.net反序列化嵌套字典

来自分类Dev

Spray-json反序列化嵌套对象

来自分类Dev

反序列化JSON字符串嵌套字典

来自分类Dev

如何反序列化嵌套的JSON数组?

来自分类Dev

基于嵌套的type属性反序列化JSON数组

来自分类Dev

使用多层嵌套反序列化JSON

来自分类Dev

如何反序列化复杂的嵌套JSON?

来自分类Dev

Django使用嵌套字典反序列化JSON请求

来自分类Dev

C#反序列化嵌套的Json返回null

来自分类Dev

反序列化具有嵌套字典的JSON文件

来自分类Dev

无法使用Retrofit反序列化嵌套JSON(Android)

来自分类Dev

喷json嵌套类无法反序列化

来自分类Dev

反序列化 Json - 嵌套参数问题

来自分类Dev

如何使用嵌套列表反序列化 Json

来自分类Dev

Newtonsoft Json 序列化/反序列化嵌套属性

来自分类Dev

使用PHP中的类型将嵌套对象序列化/反序列化为JSON

来自分类Dev

JSON嵌套序列化

来自分类Dev

使用Jackson进行JSON字符串序列化(驼峰式)和反序列化(来自蛇形)

来自分类Dev

Jackson json反序列化,带有来自json的根元素

来自分类Dev

Newtonsoft JSON不会反序列化我的json文件中的嵌套对象