在 Python 中读取 JSON 对象时遇到问题

2024-04-04

我有一个 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官方文档,但发现它有点模糊。

Thanks


In your for i in d循环,您正在循环字典的键。该字典仅包含一个顶级键,即“Sections”,这就是您只能看到该输出的原因。

我不完全理解你的 JSON 的结构,但看来你想要这样的东西:

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

这将打印出所有文章。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Python 中读取 JSON 对象时遇到问题 的相关文章

随机推荐

  • 何时创建新的 NSURLSession?

    重用的最佳实践是什么NSURLSessions 据我所知 似乎只要配置 超时 缓存策略等 不需要更改 就不需要创建新的配置 因为您可以从中生成新任务 单个也可以NSURLSession可以被整个应用程序重用吗 每个域 端点 按要求 查看文档
  • google buildpack psycopg2-binary 错误:未找到 pg_config 可执行文件

    这是我的requirements txt中的 psycopg2 binary 2 8 3 通过 r requests base in 我正在 docker 镜像中构建 我的印象是 如果我安装psycopg2 binary vs psycop
  • 从 C# 项目内的自定义文件夹获取文件

    第一次海报 也是新手 我创建了一个 C winform 应用程序 我添加了一个 文档 文件夹 其中添加了 5 个 PDF 文件 在我的 Form1 中 我添加了一个按钮 在按钮单击事件中 我尝试从 文档 文件夹中获取文件 我用谷歌搜索了一下
  • File.listFiles() 使用 JDK 6 处理 unicode 名称(Unicode 规范化问题)

    在 OS X 和 Linux 上列出 Java 6 中的目录内容时 我遇到了一个奇怪的文件名编码问题 File listFiles 和相关方法似乎以与系统其他部分不同的编码返回文件名 请注意 给我带来问题的不仅仅是这些文件名的显示 我主要对
  • Laravel 和惯性.js 文件下载

    I uploaded a file to the database and created Storage link using php artisan storage link and everything work perfectly
  • MySQL 中多个表的 COUNT(*)

    如何从 MySQL 的多个表中选择 COUNT Such as SELECT COUNT AS table1Count FROM table1 WHERE someCondition JOIN SELECT COUNT AS table2C
  • 如何获取 UnsafeMutableRawPointer 的值?

    我试图获取 UnsafeMutableRawPointer 指向的地址 但我无法这样做 我也是 Swift 新手 所以我可能会遗漏一些东西或者做得完全错误 我最好将原始值转换为 CChar 路人注意 我的大部分回答没有意义 因为它没有回答上
  • 如何更改 matplotlib 绘图上的字体大小

    如何更改 matplotlib 绘图上所有元素 刻度线 标签 标题 的字体大小 我知道如何更改刻度标签尺寸 这是通过以下方式完成的 import matplotlib matplotlib rc xtick labelsize 20 mat
  • 可以在不重新加载表视图的情况下更改 UITableViewCell 的高度吗?

    我正在尝试调整 UITableViewCell 的大小而不重新加载表视图 我的 UITableViewCell 包含一个 UITextView 因此用户可以输入长度可能为几行 最多四行 的列表项 现在 每次用户按下返回键时 我都会调整文本视
  • 如何在C#中获取网站根路径?

    在 C 代码中 我需要为图像编写 src 有谁知道如何在c 中获取网站根路径 我的文件夹结构是 UI Image 我发现当我使用 string rootpath Page Request ApplicationPath 如果在调试模式下运行
  • 使用 memset 初始化浮点数组

    这是我想尝试编写的代码 include
  • 如何使用 perl 解释器转义单引号?

    如何转义 bash 表达式中的单引号find xargs perl pi e s conflicts gt g 我想替换我的文件中的字符串 conflicts gt FatalError和gpojd都给出了很好的解决方案 我将用另一种选择来
  • 如何使用Python获取请求中响应的原始内容?

    尝试获取HTTP响应内容的原始数据requests在Python中 我有兴趣通过另一个渠道转发响应 这意味着理想情况下内容应该尽可能原始 这样做的好方法是什么 After requests get 您可以使用r content提取原始字节类
  • 如何在今天的 v8 中包含另一个 js 文件?

    我找到了一个旧的答案和后来的更新here https stackoverflow com questions 1149340 how do you include another js file in googles v8 但是我很难将此代
  • GoJS中如何动态添加Node数据和Link数据?

    myDiagram model new go GraphLinksModel key Alpha color lightblue key Delta color pink from Alpha to Alpha from Delta to
  • PHPExcel 和文本换行

    我知道这行代码将使单元格文本换行 objPHPExcel gt getActiveSheet gt getStyle D1 gt getAlignment gt setWrapText true D1 是所选的单元格 有没有办法让整个 Ex
  • 将 void* 转换为二维数组

    我有一个 void 我正在进入某个函数 它实际上是一个二维 int 数组 我想将它作为参数发送给需要二维数组的函数 正确施放它的最佳方法是什么 void foo void val How to cast val in order to se
  • Android Compose 中的 TextField 与键盘重叠

    我有一个TextField在列中verticalScroll 添加大量字符时 文本字段大小超出键盘范围 我看不到正在输入的内容 我尝试使用this https google github io accompanist insets lib
  • 从 Android UI 抽象 Firebase

    我面临的问题是 当我们将 Firebase 与 Activity 或 Fragment 连接时 Firebase 作为实时数据库效果很好 但是尝试与视图元素分离并为其创建一些抽象 因为例如我们想在测试时或将来替换它 变得相当困难 尤其是尝试
  • 在 Python 中读取 JSON 对象时遇到问题

    我有一个 JSON 对象 我试图使用 Python 读取它 但遇到一些问题 我有一个名为 test txt 的文件 其中包含收到的 JSON 对象 test txt 的内容如下 Sections Now Thursday 3 Februar