从json dictionary + python中检索值

时间:2017-04-24 12:37:56

标签: python json dictionary

这个特别的问题经常被问到,我真的尝试了所有不同的方法而没有任何运气。

这是我的json回复。我试图检索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:字符串索引必须是整数

for item in data['cuisines']:
    #print "item is" + str(item)
    for ditem in item['cuisine']:
        print ditem['cuisine_name']
        print ditem['cuisine_id']

3 个答案:

答案 0 :(得分:2)

尝试:

for item in data['cuisines']:
   for k,v in item.items():
     print('{}, {}'.format(v['cuisine_id'], v['cuisine_name']))

答案 1 :(得分:0)

for item in data['cuisines']:
    print(*item['cuisine'].values(),sep=', ')

答案 2 :(得分:0)

我想回答以下错误

  

TypeError:字符串索引必须是整数

这应该可以解决错误

for item in data['cuisines']:
    for ditem in item:
        print item[ditem]['cuisine_name']
        print item[ditem]['cuisine_id']