2013-03-30 40 views
1

我想從Python中非常複雜的JSON文件中獲取一些信息。下面是該文件只是一個對象:從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字典似乎混亂了屬性的順序,使我無法找到一個對象開始的位置,另一個開始。對於我來說,提取「標題」,「身體」和「已發佈」鍵和值的最佳方式是什麼,以及我如何爲多個對象執行操作?

+3

JSON映射和Python字典沒有排序。您只需按鍵訪問值。 –

回答

1
import json 

obj = json.loads(json_input) 

for record in obj: 
    print obj["title"] 
    print obj["body"] 
    print obj["published"] 

假設json_input是上面的代碼片段,以字符串形式或已經通過文件讀入。另外請注意,我推測上面的代碼片段是基於你的問題的一個集合。

更新基於示例

,你有沒有出現在最初發布的片段另一層。

改變循環是:

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

感謝您的回覆。試圖給了我以下錯誤: 回溯(最近通話最後一個): 文件 「/Users/Declan/Documents/Script/list.py」,14號線在 打印的obj [ 「標題」] KeyError異常:'標題'。 也許我應該包括更多的我的JSON文件。這是完整的文件:https://gist.github.com/anonymous/8b404793c4b7b97ae360。 – declanjscott

+0

@ user2226825:'在obj ['d'] ['results']中記錄:' – Eric

+0

@ user2226825我已經更新了我的答案。試試看。 – Finglas

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'] 
    )