python讀取json並解析如何操作?一起來看看小編今天的分享吧!
python讀取和解析json即解碼過程,表示把Json格式字元串解碼轉換成Python對象,可以透過loads函數實現這一過程。
例如:
#匯入json庫 import json#定義一個json字元串json_str = "[{"a":1,"b":2},{"c":3,"d":4,"e":5}]#用Loads方法讀取json字元串,並解析成Python數據結構 json_data = json.loads(json_str) json_data#輸出結果[{'a': 1, 'b': 2}, {'c': 3, 'd': 4, 'e': 5}]
json格式的數據在解析到Python中數據結構也會發生相應的變化,解析前後json與Python數據結構的對應關係如下:
JSON中數據結構——Python3中數據結構
object——dict
array——list
string——str
number (int)——int
number (real)——float
TRUE——TRUE
FALSE——FALSE
oull——None