我收到了Foursquare的回复,内容如下:
response: {
suggestedFilters: {},
geocode: {},
headerLocation: "Harlem",
headerFullLocation: "Harlem",
headerLocationGranularity: "city",
query: "coffee",
totalResults: 56,
suggestedBounds: {},
groups: [{
type: "Recommended Places",
name: "recommended",
items: [{
reasons: {
count: 1,
items: [{
summary: "You've been here 6 times",
type: "social",
reasonName: "friendAndSelfCheckinReason",
count: 0
}]
},
venue: {
id: "4fdf5edce4b08aca4a462878",
name: "The Chipped Cup",
contact: {},
location: {},
categories: [],
verified: true,
stats: {},
url: "http://www.chippedcupcoffee.com",
price: {},
hasMenu: true,
rating: 8.9,
ratingColor: "73CF42",
ratingSignals: 274,
menu: {},
allowMenuUrlEdit: true,
beenHere: {},
hours: {},
photos: {},
venuePage: {},
storeId: "",
hereNow: {}
},
tips: [],
referralId: "e-0-4fdf5edce4b08aca4a462878-0"
},
]
如果我键入以下内容:
for value in json_data['response']['groups'][0]:
print(value['name'])
我得到一个TypeError:字符串索引必须是整数
我只是想知道如何遍历此响应以获取商家名称。
由于
答案 0 :(得分:1)
for value in json_data['response']['groups']
或者您需要使用json.loads
函数
另外,我认为你想要
value['venue']['name']
答案 1 :(得分:0)
json_data['response']['groups'][0]
是一本字典。当你遍历一个字典时,你正在迭代一个键列表,所有键都是字符串......所以在循环中,value
是一个字符串。
所以当你要求value['name']
时,你试图用['name']
索引字符串,这没有任何意义,因此错误。
我认为你的意思是:
for value in json_date['response']['groups']