连接json数组;添加唯一键时出现问题

时间:2017-12-11 09:30:24

标签: python arrays json web-scraping

我正在废弃一个充满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}}'}

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

1 个答案:

答案 0 :(得分:0)

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