从JSON响应中检索数据并在Python中进行解析

时间:2019-01-13 23:09:29

标签: python json python-3.x amazon-dynamodb

我在获取数据并通过给定的JSON进行解析时遇到问题。我已经完美地检索了JSON,并通过所需的项目进行了解析,但是当我将数据返回到我的页面时,当我添加第5个项目时,它就乱了。有没有办法使用字典而不是'if'语句或使用if语句来解决此问题,以便为我提供正确的数据?

我已经尝试了许多不同的方法,例如:实现一个dict,使用json.dumps()解析数据以及其他多种方法。

shopInfo = ['shopItemTitle', 'shopItemPrice',
            'shopItemWeight', 'shopItemQuantity', 'shopItemPicture']
@app.route("/shop", methods=['GET', 'POST'])
def shop():
    shopItemTitle = []
    shopItemPrice = []
    shopItemWeight = []
    shopItemQuantity = []
    shopItemPicture = []
    shopItemPlace = []
    value = []
    res = requests.get(url=urlShopItemsGet)
    dataReturn = res.json()
    length = int(len(dataReturn))

for i in range(len(shopInfo)):
    for j in range(length):
        target = dataReturn[j]
        target = target[shopInfo[i]]
        shopItemPlace.append(target)
        if shopInfo[i] == 'shopItemTitle':
            shopItemTitle.append(shopItemPlace[j])
        elif shopInfo[i] == 'shopItemPrice':
            shopItemPrice.append(shopItemPlace[j+3])
        elif shopInfo[i] == 'shopItemWeight':
            shopItemWeight.append(shopItemPlace[j+6])
        elif shopInfo[i] == 'shopItemQuantity':
            shopItemQuantity.append(shopItemPlace[j+9])
        elif shopInfo[i] == 'shopItemPicture':
            shopItemPicture.append(shopItemPlace[j+12])
return render_template("shop.htm", dataReturn=dataReturn, value=value, shop=zip(shopItemTitle, shopItemPrice, shopItemWeight, shopItemPicture, shopItemQuantity))

给出此JSON:

[
{u'checked': False, u'shopItemQuantity': u'4', u'shopItemPrice': u'2.00', u'shopItemWeight': u'lb', u'shopItemTitle': u'TestTitle', u'shopItemPicture': u'placeholder', u'updatedAt': 1547419711871, u'id': u'59f3c952-1785-11e9-a701-ce3809c0a2a4', u'createdAt': 1547419711871}, 

{u'checked': True, u'shopItemQuantity': u'22', u'shopItemPrice': u'3.74', u'shopItemWeight': u'lb', u'shopItemTitle': u'Greens', u'shopItemPicture': u'https://s3.amazonaws.com/shopimages/shop/product1.jpg', u'updatedAt': 1547413032445, u'id': u'ccb4370c-1775-11e9-aec3-ce3809c0a2a4', u'createdAt': 1547413032445}, 

{u'checked': True, u'shopItemQuantity': u'2', u'shopItemPrice': u'3.00', u'shopItemWeight': u'lb', u'shopItemTitle': u'Okra', u'shopItemPicture': u'https://s3.amazonaws.com/shopimages/shop/product4.jpg', u'updatedAt': 1547414478896, u'id': u'2adb633e-1779-11e9-aec3-ce3809c0a2a4', u'createdAt': 1547414478896},

{u'checked': True, u'shopItemQuantity': u'3', u'shopItemPrice': u'4.30', u'shopItemWeight': u'oz', u'shopItemTitle': u'Sorrel', u'shopItemPicture':u'https://s3.amazonaws.com/shopimages/shop/product1.jpg', u'updatedAt': 1547413026608, u'id': u'c939fc10-1775-11e9-aec3-ce3809c0a2a4', u'createdAt': 1547413026608}

]

我需要像这样解析和存储数据:

shopItemTitle = ["Sorrel", "Greens", "Okra"]
shopItemPrice = ["3.00", "4.30", "3.74"]
shopItemWeight = ["oz", "lb", "lb"]
shopItemQuantity = ["2", "3", "22"]
shopItemPicture = ["https://s3.amazonaws.com/shopimages/shop/product4.jpg",                "https://s3.amazonaws.com/shopimages/shop/product1.jpg",               "https://s3.amazonaws.com/shopimages/shop/product1.jpg"]

0 个答案:

没有答案