這個特殊的問題經常被問到安靜,我真的嘗試過所有不同的方法,沒有任何運氣。從json字典中檢索值+ python
這是我的json response.And Im試圖檢索cuisine_id和cuisine_name字段的值。
{
"cuisines": [
{
"cuisine": {
"cuisine_id": 1,
"cuisine_name": "American"
}
},
{
"cuisine": {
"cuisine_id": 2,
"cuisine_name": "Andhra"
}
},
{
"cuisine": {
"cuisine_id": 4,
"cuisine_name": "Arabian"
}
},
{
"cuisine": {
"cuisine_id": 3,
"cuisine_name": "Asian"
}
},
{
"cuisine": {
"cuisine_id": 5,
"cuisine_name": "Bakery"
}
},
{
"cuisine": {
"cuisine_id": 270,
"cuisine_name": "Beverages"
}
},
{
"cuisine": {
"cuisine_id": 7,
"cuisine_name": "Biryani"
}
},
{
"cuisine": {
"cuisine_id": 168,
"cuisine_name": "Burger"
}
},
{
"cuisine": {
"cuisine_id": 30,
"cuisine_name": "Cafe"
}
},
{
"cuisine": {
"cuisine_id": 25,
"cuisine_name": "Chinese"
}
},
{
"cuisine": {
"cuisine_id": 35,
"cuisine_name": "Continental"
}
},
{
"cuisine": {
"cuisine_id": 100,
"cuisine_name": "Desserts"
}
},
{
"cuisine": {
"cuisine_id": 38,
"cuisine_name": "European"
}
},
{
"cuisine": {
"cuisine_id": 40,
"cuisine_name": "Fast Food"
}
},
{
"cuisine": {
"cuisine_id": 271,
"cuisine_name": "Finger Food"
}
},
{
"cuisine": {
"cuisine_id": 233,
"cuisine_name": "Ice Cream"
}
},
{
"cuisine": {
"cuisine_id": 55,
"cuisine_name": "Italian"
}
},
{
"cuisine": {
"cuisine_id": 164,
"cuisine_name": "Juices"
}
},
{
"cuisine": {
"cuisine_id": 62,
"cuisine_name": "Kerala"
}
},
{
"cuisine": {
"cuisine_id": 66,
"cuisine_name": "Lebanese"
}
},
{
"cuisine": {
"cuisine_id": 72,
"cuisine_name": "Mangalorean"
}
},
{
"cuisine": {
"cuisine_id": 75,
"cuisine_name": "Mughlai"
}
},
{
"cuisine": {
"cuisine_id": 50,
"cuisine_name": "North Indian"
}
},
{
"cuisine": {
"cuisine_id": 82,
"cuisine_name": "Pizza"
}
},
{
"cuisine": {
"cuisine_id": 1005,
"cuisine_name": "Roast Chicken"
}
},
{
"cuisine": {
"cuisine_id": 83,
"cuisine_name": "Seafood"
}
},
{
"cuisine": {
"cuisine_id": 85,
"cuisine_name": "South Indian"
}
},
{
"cuisine": {
"cuisine_id": 90,
"cuisine_name": "Street Food"
}
}
]
}
我能夠用下面的代碼
import requests
from pprint import pprint
import json
apiUrl = "https://<blahblah>/api/v2.1/cuisines?city_id=31"
header = {"User-agent":"curl/7.43.0", "Accept":"application/json","user-key":"API_KEY"}
response = requests.get(apiUrl, headers=header)
binary = response.content
data = json.loads(binary)
for item in data['cuisines']:
#print "item is" + str(item)
for k,v in item['cuisine'].items():
print(k,v)
輸出
(u'cuisine_name', u'American')
(u'cuisine_id', 1)
(u'cuisine_name', u'Andhra')
(u'cuisine_id', 2)
(u'cuisine_name', u'Arabian')
(u'cuisine_id', 4)
(u'cuisine_name', u'Asian')
(u'cuisine_id', 3)
(u'cuisine_name', u'Bakery')
(u'cuisine_id', 5)
(u'cuisine_name', u'Beverages')
(u'cuisine_id', 270)
(u'cuisine_name', u'Biryani')
(u'cuisine_id', 7)
(u'cuisine_name', u'Burger')
(u'cuisine_id', 168)
(u'cuisine_name', u'Cafe')
(u'cuisine_id', 30)
(u'cuisine_name', u'Chinese')
(u'cuisine_id', 25)
(u'cuisine_name', u'Continental')
(u'cuisine_id', 35)
(u'cuisine_name', u'Desserts')
(u'cuisine_id', 100)
(u'cuisine_name', u'European')
(u'cuisine_id', 38)
(u'cuisine_name', u'Fast Food')
(u'cuisine_id', 40)
(u'cuisine_name', u'Finger Food')
(u'cuisine_id', 271)
(u'cuisine_name', u'Ice Cream')
(u'cuisine_id', 233)
(u'cuisine_name', u'Italian')
(u'cuisine_id', 55)
(u'cuisine_name', u'Juices')
(u'cuisine_id', 164)
(u'cuisine_name', u'Kerala')
(u'cuisine_id', 62)
(u'cuisine_name', u'Lebanese')
(u'cuisine_id', 66)
(u'cuisine_name', u'Mangalorean')
(u'cuisine_id', 72)
(u'cuisine_name', u'Mughlai')
(u'cuisine_id', 75)
(u'cuisine_name', u'North Indian')
(u'cuisine_id', 50)
(u'cuisine_name', u'Pizza')
(u'cuisine_id', 82)
(u'cuisine_name', u'Roast Chicken')
(u'cuisine_id', 1005)
(u'cuisine_name', u'Seafood')
(u'cuisine_id', 83)
(u'cuisine_name', u'South Indian')
(u'cuisine_id', 85)
(u'cuisine_name', u'Street Food')
(u'cuisine_id', 90)
得到美食的數據,但是,我想要的是像90不同的值,街頭食品。我將如何實現它?
有沒有其他辦法?
繼變化讓我
TypeError: string indices must be integers
for item in data['cuisines']:
#print "item is" + str(item)
for ditem in item['cuisine']:
print ditem['cuisine_name']
print ditem['cuisine_id']
試着用'打印更換內部的循環(名單(項目[「美食」]。值()))' – Kendas