I've found an "TypError" and I don't know how to solve it anymore. Please, some help. I'll really appreciate an explanation if is possible.
My code:
import json
input = '''{
"text":"Sample data",
"subjects":[
{
"id":"A",
"quant":10
},
{
"id":"B",
"quant":9
},
{
"id":"C",
"quant":8
},
{
"id":"D",
"quant":7
},
{
"id":"E",
"quant":6
}]}
'''
info = json.loads(input)
count = 0
total = 0
for item in info:
value = item["subjects"][0]["quant"]
value = int(value)
total += value
count += count
print 'Count: ', count
print 'Sum: ', total
Error:
; exit; {u'text': u'Sample data', u'subjects': [{u'quant': 10, u'id': u'A'}, {u'quant': 9, u'id': u'B'}, {u'quant': 8, u'id': u'C'}, {u'quant': 7, u'id': u'D'}, {u'quant': 6, u'id': u'E'}]} Traceback (most recent call last): File "/Users/macme/Documents/Python/test_Json.py", line 61, in value = item["subjects"][0]["quant"] TypeError: string indices must be integers logout Saving session... ...copying shared history... ...saving history...truncating history files... ...completed.
[Process completed]