2013-12-09 75 views
0

我有一個JSON文件,其中包含我需要的一些數據。我想編寫一個python程序來讀取它並獲取信息。任何幫助?將JSON數據解析爲Python

這裏是JSON

var case_data = { 
    "cases": { 
    "1": { 
     "amount": 1500.0, 
     "case_id": "1", 
     "case_name": "US v. Control Systems Specialist, Inc. and Darrold Richard Crites", 
     "country": "br", 
     "sector": "sector-defense" 
    }, 
    "10": { 
     "amount": 0.0, 
     "case_id": "10", 
     "case_name": "SEC v. Int'l Systems & Controls Corp.", 
     "country": "cl", 
     "sector": "sector-agriculture" 
    } 
    }, 
    "countries": { 
    "ae": { 
     "cases": [ 
     "191", 
     "192", 
     "193", 
     "282", 
     "332" 
     ], 
     "sectors": { 
     "sector-consulting": { 
      "total": 1812113.33 
     }, 
     "sector-energy": { 
      "total": 6622147.0 
     }, 
     "sector-infrastructure": { 
      "total": 4694551.0 
     } 
     }, 
     "total": 13128811.33, 
     "tree": { 
     "children": [ 
      { 
      "children": [], 
      "classname": "sector-energy", 
      "data": { 
       "$amount": 4550000.0, 
       "$area": 3 
      }, 
      "id": "191", 
      "name": "Control Components Inc. et al." 
      }, 
      { 
      "children": [], 
      "classname": "sector-infrastructure", 
      "data": { 
       "$amount": 140551.0, 
       "$area": 2 
      }, 
      "id": "192", 
      "name": "Textron Inc." 
      }, 
      { 
      "children": [], 
      "classname": "sector-infrastructure", 
      "data": { 
       "$amount": 4554000.0, 
       "$area": 3 
      }, 
      "id": "193", 
      "name": "York International Corp." 
      }, 
      { 
      "children": [], 
      "classname": "sector-consulting", 
      "data": { 
       "$amount": 1812113.33, 
       "$area": 3 
      }, 
      "id": "282", 
      "name": "Aon Corporation" 
      }, 
      { 
      "children": [], 
      "classname": "sector-energy", 
      "data": { 
       "$amount": 2072147.0, 
       "$area": 3 
      }, 
      "id": "332", 
      "name": "Tyco Int\u2019l Ltd. et al." 
      } 
     ], 
     "data": { 
      "$amount": 0, 
      "$area": 14 
     }, 
     "id": "ae", 
     "name": "UAE" 
     } 
    }, 
    "ao": { 
     "cases": [ 
     "5", 
     "9", 
     "207", 
     "208", 
     "209" 
     ], 
     "sectors": { 
     "sector-consulting": { 
      "total": 12350000.0 
     }, 
     "sector-energy": { 
      "total": 18097043.0 
     }, 
     "sector-telecom": { 
      "total": 7080000.0 
     } 
     }, 
     "total": 37527043.0, 
     "tree": { 
     "children": [ 
      { 
      "children": [], 
      "classname": "sector-energy", 
      "data": { 
       "$amount": 302043.0, 
       "$area": 2 
      }, 
      "id": "5", 
      "name": "ABB Ltd. et al." 
      }, 
      { 
      "children": [], 
      "classname": "sector-energy", 
      "data": { 
       "$amount": 16335000.0, 
       "$area": 6 
      }, 
      "id": "9", 
      "name": "Baker Hughes Inc. et al." 
      }, 
      { 
      "children": [], 
      "classname": "sector-energy", 
      "data": { 
       "$amount": 1460000.0, 
       "$area": 3 
      }, 
      "id": "207", 
      "name": "GlobalSanteFe Corp." 
      }, 
      { 
      "children": [], 
      "classname": "sector-telecom", 
      "data": { 
       "$amount": 7080000.0, 
       "$area": 3 
      }, 
      "id": "208", 
      "name": "Alcatel-Lucent S.A. et al." 
      }, 
      { 
      "children": [], 
      "classname": "sector-consulting", 
      "data": { 
       "$amount": 12350000.0, 
       "$area": 6 
      }, 
      "id": "209", 
      "name": "Panalpina World Transport (Holding) Ltd. et al." 
      } 
     ], 
     "data": { 
      "$amount": 0, 
      "$area": 20 
     }, 
     "id": "ao", 
     "name": "Angola" 
     } 
    }, 
    }; 

我想提取的數量每個國家內「部門的能源」之後的樣本。請注意,在此示例文件中,有兩個國家'ae'和'ao'。

+0

你讀過這個:http://docs.python.org/3.3/library/json.html? – puredevotion

+1

這不是JSON,這是一個Javascript語句,它將一個名爲'case_data'的變量設置爲一個對象。 – abarnert

回答

3

如果你確實有JSON。您只需使用json模塊將其解碼爲原生dict,就像您使用JavaScript中的JSON對象將其解碼爲原生object一樣。比較這JS:

var case_data = JSON.parse(data); 

...等效的Python:

case_data = json.loads(data) 

一旦你做到了這一點,沒有JSON擔心了;它只是普通的本地對象,您可以像使用其他任何組合的字典和列表以及字符串和數字一樣訪問它。例如:

sector_energy = [country["sectors"]["sector-energy"] 
       for country in case_data["countries"]] 

不過,你已經證明我們是不是JSON可言;它是JavaScript源代碼,它將一個複雜對象分配給一個變量。你不能以任何語言將其解析爲JSON,因爲它不是。

當然,=;之間的源代碼部分不僅是有效的JavaScript代碼,而且還是有效的JSON。這也是有效的Python和有效的Ruby。但是如果你想解析這個文件和其他喜歡它的文件,你需要爲你想要解析的JSON提供哪些片段的規則。每個文件只是一個單獨的JS變量賦值?或者有些不同?

無論如何,實際上使用JSON進行語言之間的交換幾乎總是好得多,而不是使用類似JSON的類並希望它能夠工作。