【问题标题】:json decode issue with bing data (python)bing数据的json解码问题(python)
【发布时间】:2011-11-07 14:41:55
【问题描述】:

我遇到了 bing 的 json api 问题。

这是我从 api.bing.net/json.aspx 收到的 json 数据:

{"SearchResponse":{"Version":"2.2","Query":{"SearchTerms":"news"},"Translation":{"Results":[{"TranslatedTerm":"Noticias"} ]}}}

我需要解析 TranslatedTerm 值“Noticias”,但似乎我的 json 解码有问题。我正在使用这个..

           result = j.loads(bytes)
            print result['SearchResponse']['Translation']['Results']

python 给了我这个:

[{u'TranslatedTerm': u'Noticias'}]

如果我添加这样使用它:

result['SearchResponse']['Translation']['Results']["TranslatedTerm"]

python 会引发类似的错误

print result['SearchResponse']['Translation']['Results']["TranslatedTerm"]

TypeError: 列表索引必须是整数

如何将“Noticias”作为纯字符串获取?非常感谢...

【问题讨论】:

    标签: python json bing bing-api


    【解决方案1】:

    翻译Results是一个列表——大概是因为可以有很多结果。

    如果您确定只对第一个结果感兴趣,您可以这样做:

    result['SearchResponse']['Translation']['Results'][0]['TranslatedTerm']
    

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 2019-05-27
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 2018-04-15
      相关资源
      最近更新 更多