程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

21-day Python Advanced Learning Challenge punch-in - Day 3 (json standard library learning)

編輯:Python

json標准庫學習

  • JSON簡介
  • JSON長啥樣
  • JSON注意事項
  • JSON模塊
    • 1、json.loads()
    • 2、json.dumps()
    • 3、json.load()
    • 4、json.dump()

JSON簡介

JSON (全名: JavaScript Object Notation對象表示法)是一種輕量級的文本數據交換格式, JSON的數據格式其實就是python裡面的
字典格式,Faces can contain arrays enclosed in parentheses,也就是python裡面的列表.

  • JSON獨立於語言
  • JSON具有自我描述性,更易理解
  • JSON比XML更小更快,更易解析
  • Crawlers often get access data,接口數據就是JSON格式

JSON長啥樣

{

"_id": 1,
"name": "ZJ",
"age": 22,
"date": "2022.08.03"
}

JSON注意事項

  1. jsonThe keys must be enclosed in double quotes
  2. Not allowed in valuefunction,undefined,Nan,可以使用null
  3. No meaningless commas are allowed at the end

JSON模塊

Format conversion table
PythonJSONdict(字典)objectlist/tuple(列表/元組)arraystrstringint/float/int- & float-derived EnumsnumberTruetrueFalsefalseNonenullJSONPythonobjectdictarrayliststringstrnumber(int)intnumber(real)floattrueTruefalseFalsenullNone

1、json.loads()

json字符串轉換成python對象

>>> import json
>>> body = {
"Text":"You are so beautiful"}
>>> jsonob = json.dumps(body)
>>> jsonob
'{"Text": "You are so beautiful"}'
>>> json.loads(jsonob)
{
'Text': 'You are so beautiful'}

2、json.dumps()

python對象轉換成json字符串

#字符串轉換json對象
>>> import json
>>> body='{"Text":"you are nice girl"}'
>>> type(body)
<class 'str'>
>>> json.dumps(body)
'"{\\"Text\\":\\"you are nice girl\\"}"'
# dict轉換為json對象
>>> import json
>>> body = {
"Text":"You are so beautiful"}
>>> json.dumps(body)
'{"Text": "You are so beautiful"}'
>>>

3、json.load()

將python數據類型轉換並保存到json格式的文件內

4、json.dump()

將jsonThe data in the file in the format is read and converted topython類型


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved