網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

python讀取json並解析

欄目: IT科技 / 發佈於: / 人氣:9.32K
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

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}]

python讀取json並解析

json格式的數據在解析到Python中數據結構也會發生相應的變化,解析前後json與Python數據結構的對應關係如下:

JSON中數據結構——Python3中數據結構

object——dict

array——list

string——str

number (int)——int

number (real)——float

TRUE——TRUE

FALSE——FALSE

oull——None