我在检索数据和解析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"]
目前没有回答
相关问题 更多 >
编程相关推荐