开发者

Trouble reading JSON object in Python

开发者 https://www.devze.com 2023-02-08 01:38 出处:网络
I have a JSON object that I am trying to read using Python but I am having some issues. I have a file named \"test.txt\" which contains the received JSON object. The contents of \"test.txt\" are as fo

I have a JSON object that I am trying to read using Python but I am having some issues. I have a file named "test.txt" which contains the received JSON object. The contents of "test.txt" are as follows:

{ "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, "p开发者_运维问答hotoCounter": 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 } ] }}

My python script:

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

My output:

Sections

That is all I'm getting. I would like to end up with a list of "Articles" and their properties. I've tried looking at the official Python documentation but I found it to be a little vague.

Thanks


In your for i in d loop, you are looping over the keys of a dictionary. The dictionary only contains one top level key, namely 'Sections', which is why you see only that output.

I do not fully understand the structure of your JSON, but it appears you want something like this:

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

That will print all the articles out.


In your JSON data Sections is the outer-most data and occurs only once. Thus the output is also the expected output, as json.load will parse the list into a python object.

You can access the sections members by doing a for-loop for Sections, then for each section another for loop to get each article.


It should either be:

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

or be:

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

where you take the key and value into separate variables.

d is a python dictionary and you have to iterate over its keys and values.

0

精彩评论

暂无评论...
验证码 换一张
取 消