字符串索引必须是与json的整数错误

时间:2016-05-30 17:45:44

标签: python json

我正在尝试使用pythong解析json:

{  
   "document_tone":{  
      "tone_categories":[  
         {  
            "tones":[  
               {  
                  "score":0.044115,
                  "tone_id":"anger",
                  "tone_name":"Anger"
               },
               {  
                  "score":0.005631,
                  "tone_id":"disgust",
                  "tone_name":"Disgust"
               },
               {  
                  "score":0.013157,
                  "tone_id":"fear",
                  "tone_name":"Fear"
               },
               {  
                  "score":1.0,
                  "tone_id":"joy",
                  "tone_name":"Joy"
               },
               {  
                  "score":0.058781,
                  "tone_id":"sadness",
                  "tone_name":"Sadness"
               }
            ],
            "category_id":"emotion_tone",
            "category_name":"Emotion Tone"
         },
         {  
            "tones":[  
               {  
                  "score":0.0,
                  "tone_id":"analytical",
                  "tone_name":"Analytical"
               },
               {  
                  "score":0.0,
                  "tone_id":"confident",
                  "tone_name":"Confident"
               },
               {  
                  "score":0.0,
                  "tone_id":"tentative",
                  "tone_name":"Tentative"
               }
            ],
            "category_id":"language_tone",
            "category_name":"Language Tone"
         },
         {  
            "tones":[  
               {  
                  "score":0.0,
                  "tone_id":"openness_big5",
                  "tone_name":"Openness"
               },
               {  
                  "score":0.571,
                  "tone_id":"conscientiousness_big5",
                  "tone_name":"Conscientiousness"
               },
               {  
                  "score":0.936,
                  "tone_id":"extraversion_big5",
                  "tone_name":"Extraversion"
               },
               {  
                  "score":0.978,
                  "tone_id":"agreeableness_big5",
                  "tone_name":"Agreeableness"
               },
               {  
                  "score":0.975,
                  "tone_id":"emotional_range_big5",
                  "tone_name":"Emotional Range"
               }
            ],
            "category_id":"social_tone",
            "category_name":"Social Tone"
         }
      ]
   }
}

这是我试图使用以下代码从json获取音色名称和分数的代码:

import json
from watson_developer_cloud import ToneAnalyzerV3Beta
import urllib.request
import codecs
reader = codecs.getreader("utf-8")
tone_analyzer = ToneAnalyzerV3Beta(
    url='https://gateway.watsonplatform.net/tone-analyzer/api',
    username='<username>',
    password='<password>',
    version='2016-02-11')

data=json.dumps(tone_analyzer.tone(text='I am very happy'), indent=2)
print (data)

for cat in data['document_tone']['tone_categories']:
    print('Category:', cat['category_name'])
    for tone in cat['tones']:
        print('-', tone['tone_name'])

但是继续遇到错误字符串索引必须是整数。我试着在我之前的一篇文章中提出同样的问题但是在这篇文章中,我提供了更多细节。 我真的很感激任何投入。

谢谢

1 个答案:

答案 0 :(得分:2)

tone_analyzer.tone(text='I am very happy')

返回一个字典,无需使用json以任何方式修改数据,只需执行

X = tone_analyzer.tone(text='I am very happy')

请注意,您之前的问题已经recieved this exact answer