0

我正在使用如下代码检索给定商店的所有订单:

orders = ShopifyAPI::Order.find(:all, :params => {:financial_status => 'paid'})
orders.each do |order|
  order_json = order.to_json
  post_json_to_server(order_json)
end

但是由于某种原因,当我检查由 创建的 JSON 时order.to_jsondiscount_codesandclient_details属性看起来像这样:

"client_details":{"":{"accept_language":"en-US,en;q=0.8","browser_ip":"199.185.98.174","session_hash":"6b37d22ebcdf097f5ab4e1c9e596a504c4cdc4c41c4f2b29a3a7aae4ead559c3","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11"}}

"discount_codes":[{"":{"amount":"20.00","code":"CZV57KSE8VMV"}}]

为什么{"":这两个列表中都有前导?我的测试商店中是否配置错误?

这是我发送的 JSON的完整转储(使用RequestBin捕获):

{ "billing_address" : { "address1" : "asdf",
      "address2" : "",
      "city" : "asdf",
      "company" : "",
      "country" : "United States",
      "country_code" : "US",
      "first_name" : "asdf",
      "last_name" : "asdf",
      "latitude" : "45.176384",
      "longitude" : "-123.045601",
      "name" : "asdf asdf",
      "phone" : "",
      "province" : "Alaska",
      "province_code" : "AK",
      "zip" : "asdf"
    },
  "browser_ip" : "199.185.98.174",
  "buyer_accepts_marketing" : true,
  "cancel_reason" : null,
  "cancelled_at" : null,
  "cart_token" : "bbf42c99f456f9ccda30554022fec659",
  "client_details" : { "" : { "accept_language" : "en-US,en;q=0.8",
          "browser_ip" : "199.185.98.174",
          "session_hash" : "6b37d22ebcdf097f5ab4e1c9e596a504c4cdc4c41c4f2b29a3a7aae4ead559c3",
          "user_agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11"
        } },
  "closed_at" : null,
  "created_at" : "2012-07-10T17:23:53-04:00",
  "currency" : "CAD",
  "customer" : { "accepts_marketing" : true,
      "created_at" : "2012-07-10T17:23:53-04:00",
      "email" : "asdf@example.com",
      "first_name" : "asdf",
      "id" : 94366870,
      "last_name" : "asdf",
      "last_order_id" : null,
      "last_order_name" : null,
      "note" : null,
      "orders_count" : 0,
      "state" : "disabled",
      "tags" : "",
      "total_spent" : "0.00",
      "updated_at" : "2012-07-10T17:35:23-04:00"
    },
  "discount_codes" : [ { "" : { "amount" : "20.00",
            "code" : "CZV57KSE8VMV"
          } } ],
  "email" : "asdf@example.com",
  "financial_status" : "authorized",
  "fulfillment_status" : null,
  "fulfillments" : [  ],
  "gateway" : "bogus",
  "id" : 134753494,
  "landing_site" : "/",
  "landing_site_ref" : null,
  "line_items" : [ { "fulfillment_service" : "manual",
        "fulfillment_status" : null,
        "grams" : 0,
        "id" : 219421970,
        "name" : "Grass-roots methodical instruction set",
        "price" : "19.00",
        "product_id" : 95843140,
        "quantity" : 2,
        "requires_shipping" : true,
        "sku" : "",
        "title" : "Grass-roots methodical instruction set",
        "variant_id" : 224399478,
        "variant_inventory_management" : null,
        "variant_title" : null,
        "vendor" : "Shopify"
      } ],
  "name" : "#1004",
  "note" : "",
  "note_attributes" : [  ],
  "number" : 4,
  "order_number" : 1004,
  "payment_details" : { "avs_result_code" : null,
      "credit_card_bin" : "1",
      "credit_card_company" : "Bogus",
      "credit_card_number" : "XXXX-XXXX-XXXX-1",
      "cvv_result_code" : null
    },
  "processing_method" : "direct",
  "referring_site" : "",
  "shipping_address" : { "address1" : "asdf",
      "address2" : "",
      "city" : "asdf",
      "company" : "",
      "country" : "United States",
      "country_code" : "US",
      "first_name" : "asdf",
      "last_name" : "asdf",
      "latitude" : "45.176384",
      "longitude" : "-123.045601",
      "name" : "asdf asdf",
      "phone" : "",
      "province" : "Alaska",
      "province_code" : "AK",
      "zip" : "asdf"
    },
  "shipping_lines" : [ { "code" : "International Shipping",
        "price" : "20.00",
        "source" : "shopify",
        "title" : "International Shipping"
      } ],
  "subtotal_price" : "18.00",
  "tax_lines" : [  ],
  "taxes_included" : false,
  "token" : "51116b93d2d774b6c537a3bcc8861506",
  "total_discounts" : "20.00",
  "total_line_items_price" : "38.00",
  "total_price" : "38.00",
  "total_price_usd" : "37.28",
  "total_tax" : "0.00",
  "total_weight" : 0,
  "updated_at" : "2012-07-10T17:35:20-04:00"
}
4

2 回答 2

1

为了让 Order 的格式与 webhook 发送的 JSON 格式相同,我最终自己做了 - 这是代码:

ActiveResource::Base.include_root_in_json = true

orders = ShopifyAPI::Order.find(:all, :params => {:financial_status => 'paid'})

orders.each do |order|
  order_json = order.as_json
  %w(billing_address customer line_items payment_details shipping_address shipping_lines).each do |attribute|
    order_json[attribute] = order.send(attribute).as_json
  end

  if order_json['discount_codes'].length > 0
    order_json['discount_codes'] = [order_json['discount_codes'].as_json[0][nil]]
  end

  order_json['client_details'] = order_json['client_details'].as_json[nil]

  post_json_to_server(order_json)
end

通过这种方式,我能够将 JSON 转换为完全映射到 Shopify 的订单付费 webhook 发送的内容。

于 2012-07-12T15:52:43.170 回答
-1

当您使用to_json而不告诉 ActiveRecord 如何处理时,它可以做到这一点。

您可以告诉 ActiveRecord 在呈现 JSON 时包含根。

ActiveRecord::Base.include_root_in_json = true

然后你会看到

{"order":{...}} and not {"":{...}}

通常你可以只使用语法

order_json = order.to_json(:root => true)

为了得到你想要的密钥(这是根)。使用 JSON 仍然有点像在孩子们的狂欢节中行走......

于 2012-07-11T18:35:39.310 回答