我发现了一个“打字错误”,我不知道该怎么解决了。拜托,帮帮忙。如果可能的话,我将非常感谢你的解释。在
我的代码: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
错误:; 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]