Good day. I work with VK API, there is a JSON string with which I need to pull out some values.
In PHP, this was done easily:
$url = file_get_contents("https://api.vk.com/method/groups.search?q=bla-bla&access_token=<TOKEN>"); $data = json_decode($url,true); foreach ($data['response'] as $item) { $photo = $item['attachment']['photo']['src_big']; echo $photo } Trying to do by analogy on Python:
import urllib.request as urllib2 import json url = "https://api.vk.com/method/wall.get?owner_id=-bla-bla&" response = urllib2.urlopen(url) data = json.loads(response.read()) a = data['response']['attachment']['photo']['src_big'] print(a) I get the error:
a = data['response']['attachment']['photo']['src_big'] TypeError: list indices must be integers or slices, not str The maximum I can do is get all the available fields:
....... a = data['response'] for b in a: print(b) How do I write to the variable a = data['response']['attachment']['photo']['src_big'] ?
Thanks in advance!