【问题标题】:Extracting from a very complex JSON file in Python从 Python 中非常复杂的 JSON 文件中提取
【发布时间】:2013-03-30 10:14:28
【问题描述】:

我正在尝试使用 Python 从一个非常复杂的 JSON 文件中获取一些信息。以下只是文件中的一个对象:

{
"__metadata": {
"uri": "/Students/news/_vti_bin/ListData.svc/Posts(4)", "etag": "W/\"2\"", "type": "Microsoft.SharePoint.DataService.PostsItem"
}, "Title": "Term 2 Round 2 draws", "Body": "<div class=\"ExternalClass0BC1BCA4D3EE45A4A1F34086034FE827\"><p>\u200bAs there is no Gonzagan this week the following Senior Sport information has been provided here.\r\n\t    </p>\r\n<ul><li><a target=\"_blank\" href=\"/Intranet/students/news_resources/2011/Term2/Knox _wet_weather.pdf\">Knox _wet_weather</a> Cancellations, please see <a target=\"_blank\" href=\"http://www.twitter.com/SACWetWeather\">twitter page</a> for further news.</li>\r\n<li><a target=\"_blank\" href=\"/Intranet/students/news_resources/2011/Term2/2011_Football_round_2.pdf\">2011 Football draw Round 2</a></li>\r\n<li><a target=\"_blank\" href=\"/Intranet/students/news_resources/2011/Term2/2011_Rugby_round_2.pdf\">2011 Rugby draw Round 2</a></li></ul>\r\n<p></p></div>", "Category": {
"__deferred": {
"uri": "/Students/news/_vti_bin/ListData.svc/Posts(4)/Category"
}
}, "Published": "\/Date(1308342960000)\/", "ContentTypeID": "0x0110001F9F7104FDD3054AAB40D8561196E09E", "ApproverComments": null, "Comments": {
"__deferred": {
"uri": "/_vti_bin/ListData.svc/Posts(4)/Comments"
}
}, "CommentsId": 0, "ApprovalStatus": "0", "Id": 4, "ContentType": "Post", "Modified": "\/Date(1309122092000)\/", "Created": "\/Date(1309120597000)\/", "CreatedBy": {
"__deferred": {
"uri": "/Students/news/_vti_bin/ListData.svc/Posts(4)/CreatedBy"
}
}, "CreatedById": 1, "ModifiedBy": {
"__deferred": {
"uri": "/Students/news/_vti_bin/ListData.svc/Posts(4)/ModifiedBy"
}
}, "ModifiedById": 1, "Owshiddenversion": 2, "Version": "1.0", "Path": "/Students/news/Lists/Posts"
},

我无法全神贯注地编辑这个。将其转换为 python 字典似乎会混淆属性的顺序,使我无法找到一个对象的开始位置和另一个对象的开始位置。对我来说,仅提取“标题”、“正文”和“已发布”键和值的最佳方法是什么,我将如何为多个对象执行此操作?

【问题讨论】:

  • JSON 映射和 Python 字典未排序。您只需通过键访问值。

标签: python json parsing dictionary


【解决方案1】:

我假设您的主要 JSON 对象是这些对象的数组。以下是我打印出您所需要的信息的方式:

import json

main_array = json.load('my_json_file.json')

for sub_object in main_array:
    print "Title: {}\nBody: {}\nPublished: {}\n".format(
        sub_object['Title'], sub_object['Body'], sub_object['Published']
    )

【讨论】:

    【解决方案2】:
    import json
    
    obj = json.loads(json_input)
    
    for record in obj:
        print obj["title"]
        print obj["body"]
        print obj["published"]
    

    假设 json_input 是上面的 sn-p,是字符串形式,或者已经通过文件读入。另请注意,我假设上述 sn-p 是基于您的问题的集合。

    更新

    根据示例,您有另一个层,最初发布的 sn-p 中不存在。

    将循环更改为:

    for record in obj["d"]["results"]:
        ...
    

    【讨论】:

    • 感谢您的回复。尝试这给了我以下错误: Traceback(最近一次调用最后一次):文件“/Users/Declan/Documents/Script/list.py”,第 14 行,在 print obj[“title”] KeyError:'title ' .也许我应该包含更多我的 JSON 文件。这是完整的文件:gist.github.com/anonymous/8b404793c4b7b97ae360.
    • @user2226825: for record in obj['d']['results']:
    • @user2226825 应该可以。示例文件很大,我现在无法在手机上加载它。我通过 Json lint 运行它,但它在我的桌面上是有效的。每条记录都有标题吗?如果并非所有记录都具有您期望的字段,则可能值得检查或处理异常。或者,尝试仅从文件中加载一条记录。如果那行得通,那么代码就可以了,那么问题就出在数据上。尝试以for record in obj["d"]["results"][:1]: 循环,看看一条记录是否有效。
    • @Finglas 我仍然收到 "KeyError: 'Title'" 。也许我错过了一些令人难以置信的明目张胆的东西?这是我目前的代码:obj = json.loads(r.text) for record in obj["d"]["results"][:1]: print obj['Title'] print obj["Body"] print obj["Published"]
    • 感谢今晚帮助我的所有人。我已经解决了我的问题,这是我的代码: obj = json.loads(r.text) obj = obj["d"]["results"] for b in obj: print b["Title"] print b[" Published"] print b["Body"] 此代码正在处理此 JSON:gist.github.com/anonymous/8b404793c4b7b97ae360。我希望这对以后遇到类似问题的人有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多