JSON TypeError: list indices must be integers, not str
<p>I want to extract some data from JSON, but I don't know what happenend. It response "TypeError: list indices must be integers, not str". 
Here is my code, thanks:</p>

<pre><code>import urllib
import json
url = 'http://python-data.dr-chuck.net/comments_304658.json'
data = urllib.urlopen(url).read()
info = json.loads(data)
#print json.dumps(info,indent=4)
lst = list()
for item in info:
    count = item['comments']['count']
    count = int(count)
    lst.append(count)
print sum(lst)
</code></pre>

<p>You seem to be confused by the structure of the returned data. Your code assumes the structure is a list of two-level dictionaries. If this were the case, then you could find an individual <code>count</code> like so:</p>

<pre><code>info[7]['comments']['count']
</code></pre>

<p>It is actually a dictionary, one item of which is a list of dictionaries. To find a single item, the expression is like:</p>

<pre><code>info['comments'][7]['count']
</code></pre>

<p>So, if we want to iterate over the list, we iterate over <code>info['comments']</code>.</p>

<p>Try this:</p>

<pre><code>import urllib
import json
url = 'http://python-data.dr-chuck.net/comments_304658.json'
data = urllib.urlopen(url).read()
info = json.loads(data)
#print json.dumps(info,indent=4)
lst = list()
for item in info['comments']:
    count = item['count']
    count = int(count)
    lst.append(count)
print sum(lst)
</code></pre>

python json