JSON (JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,是基于ECMAScript的一個子集。
成都創(chuàng)新互聯(lián)公司是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計,網(wǎng)站模板,微信公眾號開發(fā),軟件開發(fā),成都小程序開發(fā),十余年建站對門窗定制等多個行業(yè),擁有豐富的網(wǎng)站維護經(jīng)驗。
Python3 中可以使用 json 模塊來對 JSON 數(shù)據(jù)進行編解碼,包含兩個函數(shù):
json.dumps(): 對數(shù)據(jù)進行編碼。
json.loads(): 對數(shù)據(jù)進行解碼。
在json的編解碼過程中,Python 的數(shù)據(jù)類型與json類型會相互轉(zhuǎn)換。
json.dump():將數(shù)據(jù)保存為JSON文件
json.load():從JSON文件讀取數(shù)據(jù)
Python數(shù)據(jù)類型編碼為JSON數(shù)據(jù)類型轉(zhuǎn)換表:
dict object
list,tuple array
str string
Int,float,enum number
True true
False false
None null
JSON解碼為Python數(shù)據(jù)類型轉(zhuǎn)換表:
object dict
array list
string str
number(int) int
number(real) float
true True
false False
null None
在學(xué)習(xí)過程中有什么不懂得可以加我的
python學(xué)習(xí)交流扣扣qun,784-758-214
×××里有不錯的學(xué)習(xí)視頻教程、開發(fā)工具與電子書籍。
與你分享python企業(yè)當(dāng)下人才需求及怎么從零基礎(chǔ)學(xué)習(xí)好python,和學(xué)習(xí)什么內(nèi)容
# -*- coding:utf-8 -*-
import json
data = {
"id":"123456",
"name":"Bauer",
"age":30
}
jsonFile = "data.json"
if __name__ == '__main__':
# 將字典數(shù)據(jù)轉(zhuǎn)換為JSON對象
print("raw data: ", data)
jsonObject = json.dumps(data)
print("json data: ", jsonObject)
# 將JSON對象轉(zhuǎn)換為字典類型數(shù)據(jù)
rowData = json.loads(jsonObject)
print("id: ", rowData["id"])
print("name: ", rowData["name"])
print("age: ", rowData["age"])
# 將JSON對象保存為JSON文件
with open(jsonFile, 'w') as file:
json.dump(jsonObject, file)
# 將JSON文件讀取內(nèi)容
with open(jsonFile, 'r') as file:
data = json.load(file)
print(data)
# output:
# raw data: {'id': '123456', 'name': 'Bauer', 'age': 30}
# json data: {"id": "123456", "name": "Bauer", "age": 30}
# id: 123456
# name: Bauer
# age: 30
# {"id": "123456", "name": "Bauer", "age": 30}
分享題目:Python3快速入門(八)Python3JSON
新聞來源:http://chinadenli.net/article48/jgjeep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、電子商務(wù)、App開發(fā)、全網(wǎng)營銷推廣、網(wǎng)站改版、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)