I have a response from Foursquare that reads as follows
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"
},
]
If I type the following:
for value in json_data['response']['groups'][0]:
print(value['name'])
I get a TypeError: string indices must be integers
I'm just wondering how to iterate through this response to get the names of businesses.
Thanks
You went too far. The [0] is the first element of the groups
for value in json_data['response']['groups']
Or you need to actually parse the JSON data from a string with the json.loads
function
Also, I think you want
value['venue']['name']
json_data['response']['groups'][0]
is a dictionary. When you iterate over a dictionary, you are iterating over a list of keys, all of which are strings...so within the loop, value
is a string.
So when you ask for value['name']
, you are trying to index the string with ['name']
, which doesn't make any sense, hence the error.
I think you meant:
for value in json_date['response']['groups']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.