搜尋
首頁後端開發Python教學有關Python解析JSON的內容詳解

有關Python解析JSON的內容詳解

Apr 17, 2017 am 10:41 AM
jsonpython

JSON 函數

使用 JSON 函數需要匯入 json 函式庫:import json。

函數 描述

json.dumps  將Python 物件編碼成JSON 字串

json.loads  將已編碼的JSON 字串解碼為Python 物件

json.dumps

語法

json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None , encoding="utf-8", default=None, sort_keys=False, **kw)

實例

以下實例將陣列編碼為JSON 格式資料:

#!/usr/bin/python
import json
data = {'number': 6, 'name': 'Pythontab'}
jsonData = json.dumps(data)
print jsonData

以上程式碼執行結果為:

{"number": 6, "name": "Pythontab"}

注意: 大家可能發現,執行上述轉換以後,資料並沒有發生變化,這裡要說一下: 在json中雙引號才是標註的字串分割符號,單引號不標準。

使用參數讓JSON 資料排序並格式化輸出:

#!/usr/bin/python
import json
data = {'number': 6, 'name': 'Pythontab'}
jsonData = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
print jsonData

輸出結果

{
    "name": "Pythontab",
    "number": 6
}

python 原始類型轉換到json 類型的對照表:

list, tuplearray#str, unicodestringint, long, floatnumberTruetrue
Python JSON
#dict object
##False


false

None

null

json.loads

json.loads 用於解碼JSON 資料。此函數傳回 Python 欄位的資料類型。

語法json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[,kw]]]] **kw]]]] ]])實例以下實例展示了Python 如何解碼JSON 物件:以上程式碼執行結果為:json 類型轉換到python 的型別對照表:JSON##Pythonobjectdict#arrayliststring
#!/usr/bin/python
import json
jsonData = '{"number": 6, "name": "Pythontab"}'
str = json.loads(jsonData)
print str
{u'number': 6, u'name': u'Pythontab'}

unicode

number (int)

int, long

number (real)

float

true

True

false

#False

######################## #######null######None##################使用第三方函式庫:Demjson######Demjson 是python 的第三方模組庫,可用於編碼和解碼JSON 數據,包含了JSONLint 的格式化及校驗功能。 ######Github 位址:https://github.com/dmeranda/demjson######環境配置######在使用Demjson 編碼或解碼JSON 資料前,我們需要先安裝Demjson 模組。 ######方法1:原始碼安裝######$ tar -xvzf demjson-2.2.4.tar.gz######$ cd demjson-2.2.2.4######$ python setup.py install######方法2:直接使用pip安裝######pip install Demjson######JSON 函數######函數 描述#######encode將Python 物件編碼成JSON 字串######decode  可以使用demjson.decode() 函數解碼JSON 資料。此函數傳回 Python 欄位的資料類型。 ######encode語法######demjson.encode(self, obj, nest_level=0)#########demjson.encode(self, obj, nest_level=0)######decode語法######demjson.decode(self, txt)## ####使用都很簡單,我就不在這裡舉例~~###

以上是有關Python解析JSON的內容詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Numpy數組與使用數組模塊創建的數組有何不同?Numpy數組與使用數組模塊創建的數組有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模塊與Python中的數組有何關係?CTYPES模塊與Python中的數組有何關係?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

在Python的上下文中定義'數組”和'列表”。在Python的上下文中定義'數組”和'列表”。Apr 24, 2025 pm 03:41 PM

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

Python列表是可變還是不變的?那Python陣列呢?Python列表是可變還是不變的?那Python陣列呢?Apr 24, 2025 pm 03:37 PM

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python vs. C:了解關鍵差異Python vs. C:了解關鍵差異Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python vs.C:您的項目選擇哪種語言?Python vs.C:您的項目選擇哪種語言?Apr 21, 2025 am 12:17 AM

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

達到python目標:每天2小時的力量達到python目標:每天2小時的力量Apr 20, 2025 am 12:21 AM

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。