python之 json模組,pythonjson模組

來源:互聯網
上載者:User

python之 json模組,pythonjson模組

  

import  json

data={
"no":1,
"name":"Runoob",
"url":"http://www.runoob.com"
}

json_str=json.dumps(data) # json.dumps() 函數對資料進行編碼,將其轉換成為json格式的資料
print("python 未經處理資料:",repr(data))
print("python json 資料:",json_str)
python_data=json.loads(json_str) # json.loads() 函數就是講json格式的資料解碼轉換成為python對應格式的資料
print(python_data)


輸出對應的結果:

python 未經處理資料: {'no': 1, 'name': 'Runoob', 'url': 'http://www.runoob.com'}


python json 資料: {"no": 1, "name": "Runoob", "url": "http://www.runoob.com"}


{'no': 1, 'name': 'Runoob', 'url': 'http://www.runoob.com'}

 

 

 

# 這個是 講格式化的資料寫入檔案之中和讀取檔案之中的資料
f=open("data.txt","w",encoding="utf-8")
f.write(json.dumps(data))
f.close()
f1=open("data.txt","r",encoding="utf-8")
f1=f1.read()
json_str1=json.loads(f1)
print("-----")
print(json_str1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.