【问题标题】:How to parse JSON results from Unirest call in python?如何在 python 中解析 Unirest 调用的 JSON 结果?
【发布时间】:2025-12-26 14:25:12
【问题描述】:
      response_post =unirest.get("http://timesofindia.indiatimes.com/feeds/newsdefaultfeeds.cms?feedtype=sjson",headers={
"X-Mashape-Key": "key",
"Accept": "application/json"},)

如何将 response_post 解析为 json 结果?

【问题讨论】:

    标签: python json unirest


    【解决方案1】:

    您是否尝试将 .body 添加到请求的末尾?

    文档看起来很清楚 - http://unirest.io/python.html

    response_post =unirest.get("http://timesofindia.indiatimes.com/feeds/newsdefaultfeeds.cms?feedtype=sjson",headers={
        "X-Mashape-Key": "key",
        "Accept": "application/json"},).body
    

    【讨论】:

    • 添加了 .body 并获得了相同的结果。如何迭代以获取 json 数据?比如:for i in x: for ii in i: print ii,,, 那应该给我一个特定的项目。它不像我想的那样工作。
    • 一般你想试试 print(response_post),然后 for i 在 response post: print(i) 这可以帮助你确定变量的内容是什么,以及你可以用它做什么。 - 但话虽如此,您需要使用新代码更新您的帖子,以及您得到什么输出,这样我们就不必猜测问题是什么。
    • 如果你真的从那个请求中得到了 json,你最终会得到一个可迭代的对象。粘贴 print(response_post) 的结果,我可以帮助您了解它是如何工作的。