【问题标题】:JSON could not be parsed error in Python在 Python 中无法解析 JSON 错误
【发布时间】:2014-07-29 16:49:19
【问题描述】:

我正在尝试编写一个 Python 脚本来使用 Zinc API 进行亚马逊购买(请参阅:https://zinc.io/docs/#full-api)。我应该能够通过一个 POST 请求来做到这一点,所以我阅读了有关 urllib 和 urllib2 here 的信息。

但是,当我运行我的脚本时,我收到了这个错误:

{"_type":"error","code":"invalid_json","message":"The JSON in your request could not be parsed."}

我非常精确地遵循 Zinc 的代码,所以我不确定为什么会出现错误。

这是我正在运行的内容(注意:我的个人信息不包括在内,但在我运行脚本时包括在内):

import urllib
import urllib2
import json

url = 'https://api.zinc.io/v0/order'
values = {"client_token" : "public",
          "retailer" : "amazon",
          "products" :[{"product_id" : "0679753354", "quantity" : 1}],
          "max_price" : 1700, "shipping_address" : {
              "first_name" : "NAME",
              "last_name" : "NAME",
              "address_line1" : "ADDRESS",
              "address_line2" : "",
              "zip_code" : "ZIP",
              "city" : "CITY",
              "state" : "STATE",
              "country" : "US"
              },
          "is_gift" : False,
          "shipping_method" : "cheapest",
          "payment_method" : {
              "name_on_card" : "NAME",
              "number" : "CARDNUMBER",
              "security_code" : "SECCODE",
              "expiration_month" : 1,
              "expiration_year": 2015
              },
          "billing_address" : {
              "first_name" : "NAME",
              "last_name" : "NAME",
              "address_line1" : "ADDRESS",
              "address_line2" : "",
              "zip_code" : "ZIP",
              "city" : "CITY",
              "state" : "STATE",
              "country" : "US",
              "phone_number" : "PHONE"
              },
          "retailer_credentials" : {
              "email" : "EMAIL",
              "password" : "PASSWORD"}
          }

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

print the_page

任何关于如何解决这个 JSON 解析问题的想法都将不胜感激,因为我无法找出问题所在。

【问题讨论】:

  • 嗨 fnwtp,我希望你做得很好。我正在尝试使用 ZINC api 构建一个应用程序,我很好奇它到目前为止如何为您工作? C您从哪里提到“我非常精确地遵循 Zinc 的代码”?你能告诉我我需要遵循的步骤吗?我不熟悉python。提前谢谢!

标签: python json http post


【解决方案1】:

您不想对这些数据进行 urlencode。这是针对表单值,而不是 JSON。

此外,您已经导入了 json 模块,但您没有在数据上使用它。你应该这样做:

data = json.dumps(values)
req = urllib2.Request(url, data, {'content-type': 'application/json'})

虽然使用第三方 requests 库来做这类事情要容易得多,效果更好。

【讨论】:

  • 谢谢,丹尼尔。这完美地工作。我一定会检查请求库。干杯
【解决方案2】:

您正在对请求进行 url 编码,但没有将 Python 数据转换为服务器可以理解的 JSON。而不是调用 urllib.urlencode(values),你应该调用 json.dumps(values)。

例如:

import urllib
import urllib2
import json

url = 'https://api.zinc.io/v0/order'
values = {"client_token" : "public",
          "retailer" : "amazon",
          "products" :[{"product_id" : "0679753354", "quantity" : 1}],
          "max_price" : 1700, "shipping_address" : {
              "first_name" : "NAME",
              "last_name" : "NAME",
              "address_line1" : "ADDRESS",
              "address_line2" : "",
              "zip_code" : "ZIP",
              "city" : "CITY",
              "state" : "STATE",
              "country" : "US"
              },
          "is_gift" : False,
          "shipping_method" : "cheapest",
          "payment_method" : {
              "name_on_card" : "NAME",
              "number" : "CARDNUMBER",
              "security_code" : "SECCODE",
              "expiration_month" : 1,
              "expiration_year": 2015
              },
          "billing_address" : {
              "first_name" : "NAME",
              "last_name" : "NAME",
              "address_line1" : "ADDRESS",
              "address_line2" : "",
              "zip_code" : "ZIP",
              "city" : "CITY",
              "state" : "STATE",
              "country" : "US",
              "phone_number" : "PHONE"
              },
          "retailer_credentials" : {
              "email" : "EMAIL",
              "password" : "PASSWORD"}
          }

req = urllib2.Request(url, json.dumps(values))
response = urllib2.urlopen(req)
the_page = response.read()

print the_page

【讨论】:

    猜你喜欢
    • 2013-05-10
    • 2013-08-11
    • 1970-01-01
    • 2015-04-19
    • 2014-04-10
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2020-07-13
    相关资源
    最近更新 更多