【问题标题】:Concatenate json array; issue when adding a unique key连接json数组;添加唯一键时的问题
【发布时间】:2017-12-11 09:30:23
【问题描述】:

我正在废弃一个充满 JSON 数组的网站。我正在尝试连接它们并为每个数组添加一个唯一键(请参见下面的代码):

对于每次迭代,机器人调用函数测试,如果我打印 get_text,我有这个:

{"status":{"code":0,"message":"Ok","user":{"isBanned":false,"isNotConfirmed":false}},"payload":{"vat":0,"price":{"201":{"100":{"batchsize_id":60916,"quantity":100,"product_price":14.12,"shipment_price":8.98,"country_id":"DE"},"200":{"batchsize_id":60922,"quantity":200,"product_price":19.13,"shipment_price":9.06,"country_id":"DE"},"300":{"batchsize_id":60928,"quantity":300,"product_price":23.64,"shipment_price":9.14,"country_id":"DE"},"400":{"batchsize_id":60934,"quantity":400,"product_price":28.4,"shipment_price":9.23,"country_id":"DE"},"500":{"batchsize_id":60940,"quantity":500,"product_price":32.93,"shipment_price":9.32,"country_id":"DE"},"600":{"batchsize_id":60946,"quantity":600,"product_price":37.08,"shipment_price":9.4,"country_id":"DE"},"700":{"batchsize_id":60952,"quantity":700,"product_price":41,"shipment_price":9.48,"country_id":"DE"},"800":{"batchsize_id":60958,"quantity":800,"product_price":44.72,"shipment_price":9.63,"country_id":"DE"},"900":{"batchsize_id":60964,"quantity":900,"product_price":48.24,"shipment_price":9.71,"country_id":"DE"},"1000":{"batchsize_id":60970,"quantity":1000,"product_price":51.59,"shipment_price":9.79,"country_id":"DE"},"minDeliveryDays":5,"maxDeliveryDays":8}},"productionCountry":["DE"],"minDeliveryDays":5,"maxDeliveryDays":8},"pager":{"total":null,"count":null,"current":1}}

我下面的代码尝试连接每次迭代并为每个数组添加一个唯一键。

def test(url_final):
        get_url = requests.get(url_final)
        #get_dict = json.loads(get_url.text)
        get_text = get_url.text
        print(get_text)
        dictionary['a' + str(uuid.uuid4())[:8]] = get_text
        print(dictionary)

printit()

我唯一的问题是“打印字典”返回给我这个:

{'a6a465b1a': '{"status":{"code":0,"message":"Ok","user":{"isBanned":false,"isNotConfirmed":false}},"payload":{"vat":0,"price":{"201":{"100":{"batchsize_id":60916,"quantity":100,"product_price":14.12,"shipment_price":8.98,"country_id":"DE"},"200":{"batchsize_id":60922,"quantity":200,"product_price":19.13,"shipment_price":9.06,"country_id":"DE"},"300":{"batchsize_id":60928,"quantity":300,"product_price":23.64,"shipment_price":9.14,"country_id":"DE"},"400":{"batchsize_id":60934,"quantity":400,"product_price":28.4,"shipment_price":9.23,"country_id":"DE"},"500":{"batchsize_id":60940,"quantity":500,"product_price":32.93,"shipment_price":9.32,"country_id":"DE"},"600":{"batchsize_id":60946,"quantity":600,"product_price":37.08,"shipment_price":9.4,"country_id":"DE"},"700":{"batchsize_id":60952,"quantity":700,"product_price":41,"shipment_price":9.48,"country_id":"DE"},"800":{"batchsize_id":60958,"quantity":800,"product_price":44.72,"shipment_price":9.63,"country_id":"DE"},"900":{"batchsize_id":60964,"quantity":900,"product_price":48.24,"shipment_price":9.71,"country_id":"DE"},"1000":{"batchsize_id":60970,"quantity":1000,"product_price":51.59,"shipment_price":9.79,"country_id":"DE"},"minDeliveryDays":5,"maxDeliveryDays":8}},"productionCountry":["DE"],"minDeliveryDays":5,"maxDeliveryDays":8},"pager":{"total":null,"count":null,"current":1}}'}

如何删除每个数组前面的字符串?

【问题讨论】:

  • 为什么输出有问题?它与您的预期有何不同?
  • 您的实际问题是什么?请看How do I ask a good question?
  • 抱歉,我刚刚编辑了问题

标签: python arrays json web-scraping


【解决方案1】:

您将 JSON 作为字符串返回,而不是像您期望的那样作为字典。为了将字符串转换为某些值,必须对其进行解析。你有正确的电话,只是在错误的地方。一旦你填充了get_text(你可能想考虑更好的名字),你需要运行json_values = json.loads(get_text)。现在json_values 将包含您期望的字典,您可以分配它而不是get_text

【讨论】:

  • 谢谢!但现在我有一个新问题。 Json 发生了变化,{'aa0f2fb7b': {'payload': {'minDeliveryDays': 5.... }}} 它应该以状态开头,这是为什么呢?没听懂?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-09
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 2020-02-17
  • 1970-01-01
相关资源
最近更新 更多