【问题标题】:Trouble reading JSON object in Python在 Python 中读取 JSON 对象时遇到问题
【发布时间】:2011-02-03 08:16:09
【问题描述】:

我有一个 JSON 对象,我正在尝试使用 Python 读取它,但我遇到了一些问题。我有一个名为“test.txt”的文件,其中包含接收到的 JSON 对象。 “test.txt”的内容如下:

{ "Sections": {"Now": "Thursday 3 February 2011 08:31",  "Section": [ { "Article": [ {"audioCounter": 0, "commentsCounter": 0, "Exceprt": "Category 5 cyclone slams into flood-hit Queensland", "hasMore": "true", "ID": 44871, "important": "False", "likesCounter": 0, "photoCounter": 0, "time": "20:58", "timeStamp": "2\/2\/2011 8:59:37 PM", "timeStatus": "True", "Title": "Category 5 cyclone slams into flood-hit Queensland", "Type": "Politics", "videoCounter": 0, "viewsCounter": 2 }, {"audioCounter": 0, "commentsCounter": 0, "Exceprt": "The White House: Egypt must begin a peaceful and orderly transition of power immediately", "hasMore": "false", "ID": 44868, "important": "True", "likesCounter": 0, "photoCounter": 0, "time": "20:51", "timeStamp": "2\/2\/2011 8:52:28 PM", "timeStatus": "True", "Title": "The White House: Egypt must begin a peaceful and orderly transition of power immediately", "Type": "Politics", "videoCounter": 0, "viewsCounter": 0 }, {"audioCounter": 0, "commentsCounter": 0, "Exceprt": "Bazzi: Berri endeavors to facilitate cabinet formation", "hasMore": "true", "ID": 44866, "important": "False", "likesCounter": 0, "photoCounter": 0, "time": "20:47", "timeStamp": "2\/2\/2011 8:48:18 PM", "timeStatus": "True", "Title": "Bazzi: Berri endeavors to facilitate cabinet formation", "Type": "Politics", "videoCounter": 0, "viewsCounter": 0 }, {"audioCounter": 0, "commentsCounter": 0, "Exceprt": "Saker via Future News: Opening files era has ended, Gen. Aoun can\u0027t open any corruption files since his allies were the pioneers of corruption", "hasMore": "false", "ID": 44865, "important": "False", "likesCounter": 0, "photoCounter": 0, "time": "20:41", "timeStamp": "2\/2\/2011 8:45:36 PM", "timeStatus": "True", "Title": "Saker via Future News: Opening files era has ended, Gen. Aoun can\u0027t open any corruption files since his allies were the pioneers of corruption", "Type": "Politics", "videoCounter": 0, "viewsCounter": 0 }, {"audioCounter": 0, "commentsCounter": 0, "Exceprt": "White House deplores violence in Egypt", "hasMore": "true", "ID": 44857, "important": "False", "likesCounter": 0, "photoCounter": 0, "time": "20:28", "timeStamp": "2\/2\/2011 8:29:26 PM", "timeStatus": "True", "Title": "White House deplores violence in Egypt", "Type": "Politics", "videoCounter": 0, "viewsCounter": 1 }, {"audioCounter": 0, "commentsCounter": 0, "Exceprt": "Baabda sources via MTV: President Suleiman works on a consensus curriculum hoping for the formation of a national unity cabinet, if this does not happen, the cabinet will be political inlaid with technocrats", "hasMore": "false", "ID": 44855, "important": "False", "likesCounter": 0, "photoCounter": 0, "time": "20:20", "timeStamp": "2\/2\/2011 8:23:14 PM", "timeStatus": "True", "Title": "Baabda sources via MTV: President Suleiman works on a consensus curriculum hoping for the formation of a national unity cabinet, if this does not happen, the cabinet will be political inlaid with technocrats", "Type": "Politics", "videoCounter": 0, "viewsCounter": 0 }, {"audioCounter": 0, "commentsCounter": 0, "Exceprt": "An American senior official to Reuters: expects pressure on Mubarak by the army after street violence", "hasMore": "false", "ID": 44853, "important": "True", "likesCounter": 0, "photoCounter": 0, "time": "20:18", "timeStamp": "2\/2\/2011 8:20:14 PM", "timeStatus": "True", "Title": "An American senior official to Reuters: expects pressure on Mubarak by the army after street violence", "Type": "Politics", "videoCounter": 0, "viewsCounter": 0 }, {"audioCounter": 0, "commentsCounter": 0, "Exceprt": "The disappearance of the Al-Arabia correspondence Ahmed Abdullah in Egypt after clashing with Mubarak supporters", "hasMore": "false", "ID": 44851, "important": "True", "likesCounter": 0, "photoCounter": 0, "time": "20:17", "timeStamp": "2\/2\/2011 8:18:28 PM", "timeStatus": "True", "Title": "The disappearance of the Al-Arabia correspondence Ahmed Abdullah in Egypt after clashing with Mubarak supporters", "Type": "Politics", "videoCounter": 0, "viewsCounter": 0 } ], "ID": 22, "Name": "EN Live", "totalNews": 2997 } ] }}

我的python脚本:

import json;
f = open("test.txt");
d = json.load(f);
for i in d:
    print(i);

我的输出:

Sections

这就是我所得到的。我想最终列出“文章”及其属性。我尝试查看官方 Python 文档,但发现它有点模糊。

谢谢

【问题讨论】:

    标签: python json


    【解决方案1】:

    在您的for i in d 循环中,您正在遍历字典的键。该字典仅包含一个顶级键,即“Sections”,这就是为什么您只能看到该输出。

    我不完全理解您的 JSON 的结构,但您似乎想要这样的东西:

    for aritcle in d['Sections']['Section'][0]['Article']:
          print article
    

    这会将所有文章打印出来。

    【讨论】:

    • 哇!感谢您的及时答复!这解决了我的问题!我仍然从 Python 开始,我还没有完全理解 dict 结构。再次感谢!
    • 没问题 - 另一个巧妙的技巧是大多数(全部?)JSON 也是有效的 Python 代码。您可以直接将 JSON 复制并粘贴到 Python 解释器中。这对实验很好。但是,当涉及到应用程序时,最好坚持使用 json 模块。
    • 并非所有 JSON 代码都是有效的 Python,但大部分都是有效的。例如,JSON 将布尔值 True/False 映射为 true/false,将 Python 的 None 映射为 null。毕竟,它是“Javascript 对象表示法”。 :)
    • @jellybean,啊,好点子,我知道我会忘记这样的事情:)
    【解决方案2】:

    在您的 JSON 数据中,Sections 是最外层的数据,并且只出现一次。因此输出也是预期的输出,json.load 会将列表解析为 python 对象。

    您可以通过对 Sections 执行 for 循环来访问 section 成员,然后为每个 section 执行另一个 for 循环来获取每篇文章。

    【讨论】:

      【解决方案3】:

      应该是:

      for i in d.items():
          print(i);
      

      或者是:

      for k,v in d.items():
          print k, v
      

      您将键和值放入单独的变量中。

      d 是一个 python 字典,你必须遍历它的键和值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 2014-12-05
        • 1970-01-01
        相关资源
        最近更新 更多