此为历史版本和 IPFS 入口查阅区,回到作品页
為自己Coding
IPFS 指纹 这是什么

作品指纹

寫給自己的技術筆記 - 作為程式開發者我們絕對不能忽略的JSON - Python 如何處理JSON文件

為自己Coding
·
·


如果覺得我的圖不清楚都可以直接在我的Github上看喔,Github連結


哈囉,由於JSON系列的第一篇(寫給自己的技術筆記 - 作為程式開發者我們絕對不能忽略的JSON - 教學和用法 - 以JavaScript為例(一))已經針對JSON做過介紹了,所以這篇就不會再詳細介紹JSON囉,這篇會把重點放在如何使用Python來處理JSON檔案


攝影師:Mateusz Sałaciak,連結:Pexels



1. JSON 介紹

  • JSON 全名為 JavaScript Object Notation,即 JavaScript 對象標記法
  • 2001年提出
  • 特性:
  • 輕量型(Light-Weight): 表示產出的檔案非常小
  • 基於文本的(Text-Based): 可以使用文本編譯器(ex. 記事本)開啟
  • 可讀的(Human-Readable)格式: 可以輕鬆被人閱讀
  • 穩定性(Stability): JSON創始人說 JSON 格式永遠不會改變,也就是說很久以前寫的 JSON 文件,很久以後也能使用,就不會有兼容性的問題
  • 目前 REST API 的主流形式,可見它在數據交換格式的重要性



2. Python中的JSON套件

  • json套件: Python中用來編碼和解碼JSON文件的套件
  • json套件為Python中自帶的套件,所以不用額外安裝,只需要直接載入即可


JSON 和 Python 資料型態轉換表

Python 轉 JSON


JSON 轉 Python

3. Python 如何處理 JSON 套件

處理過程分成:

  • Marshalling / Encoder 序列化: 代表將 Python 的物件資料轉換成為 JSON 物件,使用方法 - json.dumps 或 json.dump
  • Unmarshalling / Decoder 反序列化: 代表將 JSON 物件轉換成為 Python 的資料物件,使用方法 - json.loads 或 json.load

使用方法



完整參數

  • json.dumps
dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
  • json.dump
dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
  • json.loads
loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
  • json.load
load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)



4. 實作


JSON 檔案或字符串 轉 Python字典


1. 從JSON檔中導入

  • 我們有一份JSON擋 - test.json
{
  "Student 1": {
    "Student_id": 126,
    "Name": "Jack",
    "Score": 99,
    "Friends": ["Tim", "Jen", "Ken"]
 },
​
  "Student 2": {
    "Student_id": 128,
    "Name": "Ken",
    "Score": 99,
    "Friends": ["Jack", "Jessy", "Cathy"]
 }
}
​
  • 方法一: json.load
import json
​
## 讀取JSON檔
with open('test.json', 'r') as f:
  ## 轉成Python Dict
  python_dict = json.load(fp = f)
print("Python Dict: ", python_dict)
print("Type: ", type(python_dict))


執行結果

Python Dict: {'Student 1': {'Student_id': 126, 'Name': 'Jack', 'Score': 99, 'Friends': ['Tim', 'Jen', 'Ken']}, 'Student 2': {'Student_id': 128, 'Name': 'Ken', 'Score': 99, 'Friends': ['Jack', 'Jessy', 'Cathy']}}
Type: <class 'dict'>



  • 方法二: json.loads
​
import json
​
## 讀取JSON檔
with open('test.json', 'r') as f:
  ## 將內容裝進text
  text = f.read()
  
print("JSON Format: ", text)
print("Type: ", type(text))
print("From JSON String Transform To Python Dict")
print("Python Dict: ", json.loads(text))
print("Type: ", type(json.loads(text)))
​


執行結果

​JSON Format: {
 "Student 1": {
   "Student_id": 126,
   "Name": "Jack",
   "Score": 99,
   "Friends": ["Tim", "Jen", "Ken"]
 },
​
 "Student 2": {
   "Student_id": 128,
   "Name": "Ken",
   "Score": 99,
   "Friends": ["Jack", "Jessy", "Cathy"]
 }
}
​
Type: <class 'str'>
From JSON String Transform To Python Dict
Python Dict: {'Student 1': {'Student_id': 126, 'Name': 'Jack', 'Score': 99, 'Friends': ['Tim', 'Jen', 'Ken']}, 'Student 2': {'Student_id': 128, 'Name': 'Ken', 'Score': 99, 'Friends': ['Jack', 'Jessy', 'Cathy']}}
Type: <class 'dict'>





2. 有一筆JSON Format的字符串 轉 Python Dict

  • 只能使用json.loads
import json
​
## JSON Format的字符串
text = '{"Name":"Jack", "Score": 99}'
​
print("JSON Format: ", text)
print("Type:, "type(text))
print("From JSON String Transform To Python Dict")
print("Python Dict: ", json.loads(text))
print("Type: ", type(json.loads(text)))
​


執行結果

JSON Format: {"Name":"Jack", "Score": 99}
Type: <class 'str'>
From JSON String Transform To Python Dict
Python Dict: {'Name': 'Jack', 'Score': 99}
Type: <class 'dict'>


Python 字典(Dict) 轉 JSON字符串


1. 寫入到JSON檔 - 會自動保存成JSON檔

  • 方法一: json.dump
import json
​
## Python 字典
python_dict = {
  'Name': "Jack",
  'Score': 99
}
​
## 存成JSON File
with open("python2json.json", 'w') as f:
  json.dump(python_dict, f) 


  • 方法二: JSON.dumps
import json
​
## Python 字典
python_dict = {
  'Name': "Jack",
  "Score": 99,
}
print("Python Dict: ", python_dict)
print("Type: ", type(python_dict))
print("From Python Dict Transform To JSON Format")
print("JSON Format: ", json.dumps(python_dict))
print("Type: ", type(json.dumps(python_dict)))
​
## 存成JSON File
with open('python2json1.json', 'w') as f:
  f.write(json.dumps(python_dict))



執行結果

Python Dict: {'Name': 'Jack', 'Score': 99}
Type: <class 'dict'>
From Python Dict Transform To JSON Format
JSON Format: {"Name": "Jack", "Score": 99}
Type: <class 'str'>



2. 轉成 JSON Format的字符串

  • 只能使用函數: json.dumps
import json
​
## Python 字典
python_dict = {
  'Name': "Jack",
  "Score": 99,
}
print("Python Dict: ", python_dict)
print("Type: ", type(python_dict))
print("From Python Dict Transform To JSON Format")
print("JSON Format: ", json.dumps(python_dict))
print("Type: ", type(json.dumps(python_dict)))


執行結果

Python Dict: {'Name': 'Jack', 'Score': 99}
Type: <class 'dict'>
From Python Dict Transform To JSON Format
JSON Format: {"Name": "Jack", "Score": 99}
Type: <class 'str'>




Python 字典(Dict)中包含list 轉 JSON 字符串


  • 範例: 這邊是使用dumps來存進檔案
import json
​
## Python 字典
python_dict = {
  "Student A": {
    "Student_id": 126,
    "Name": "Jack",
    "Score": 99,
    "Friends": ["Tim", "Jen", "Cindy"]
 },
  
  "Student B":{
    "Student_id": 128,
    "Name": "Ken",
    "Score": 98,
    "Friends": ["Jack", "Jessy", "Cathy"]
 }
}
print("Python Dict: ", python_dict)
print("Type: ", type(python_dict))
print("From Python Dict Transform To JSON Format")
print("JSON Format: ", json.dumps(python_dict))
print("Type: ", type(json.dumps(python_dict)))
​
## 存成JSON File
with open('python2json2.json', 'w') as f:
  f.write(json.dumps(python_dict))


執行結果

Python Dict: {'Student A': {'Student_id': 126, 'Name': 'Jack', 'Score': 99, 'Friends': ['Tim', 'Jen', 'Cindy']}, 'Student B': {'Student_id': 128, 'Name': 'Ken', 'Score': 98, 'Friends': ['Jack', 'Jessy', 'Cathy']}}
Type: <class 'dict'>
From Python Dict Transform To JSON Format
JSON Format: {"Student A": {"Student_id": 126, "Name": "Jack", "Score": 99, "Friends": ["Tim", "Jen", "Cindy"]}, "Student B": {"Student_id": 128, "Name": "Ken", "Score": 98, "Friends": ["Jack", "Jessy", "Cathy"]}}
Type: <class 'str'>




大家也可以試試用json.dump的方式存成JSON檔喔



5. 重要補充: 如果寫進JSON檔,想要更易讀的格式來寫


  • 前面的範例中,當我們把Python Dict轉成JSON Format的字符串時,都會變成一排(行),但實際上的狀況是我們需要閱讀這些JSON檔,如果數據一多就會很難閱讀
  • 在json.dumps()裡面填上一個參數indent,用來指定每行開頭要空幾格
  • 範例一: 我將indent設4,代表空四格
import json
​
## Python 字典
python_dict = {
  'Name': "Jack",
  "Score": 99,
}
print("Python Dict: ", python_dict)
print("Type: ", type(python_dict))
print("From Python Dict Transform To JSON Format")
print("JSON Format: ", json.dumps(python_dict))
print("Type: ", type(json.dumps(python_dict)))
​
## 存成JSON File
with open('python2json3.json', 'w') as f:
  f.write(json.dumps(python_dict, indent = 4))


執行結果

Python Dict: {'Name': 'Jack', 'Score': 99}
Type: <class 'dict'>
From Python Dict Transform To JSON Format
JSON Format: {"Name": "Jack", "Score": 99}
Type: <class 'str'>





  • json.dump也擁有參數indent喔
  • 範例二:
import json
​
## Python 字典
python_dict = {
  "Student A": {
    "Student_id": 126,
    "Name": "Jack",
    "Score": 99,
    "Friends": ["Tim", "Jen", "Cindy"]
 },
  
  "Student B":{
    "Student_id": 128,
    "Name": "Ken",
    "Score": 98,
    "Friends": ["Jack", "Jessy", "Cathy"]
 }
}
print("Python Dict: ", python_dict)
print("Type: ", type(python_dict))
print("From Python Dict Transform To JSON Format")
print("JSON Format: ", json.dumps(python_dict))
print("Type: ", type(json.dumps(python_dict)))
​
## 存成JSON File
with open('python2json4.json', 'w') as f:
  f.write(json.dumps(python_dict, indent = 4))
  f.write('\n')


執行結果

Python Dict: {'Student A': {'Student_id': 126, 'Name': 'Jack', 'Score': 99, 'Friends': ['Tim', 'Jen', 'Cindy']}, 'Student B': {'Student_id': 128, 'Name': 'Ken', 'Score': 98, 'Friends': ['Jack', 'Jessy', 'Cathy']}}
Type: <class 'dict'>
From Python Dict Transform To JSON Format
JSON Format: {"Student A": {"Student_id": 126, "Name": "Jack", "Score": 99, "Friends": ["Tim", "Jen", "Cindy"]}, "Student B": {"Student_id": 128, "Name": "Ken", "Score": 98, "Friends": ["Jack", "Jessy", "Cathy"]}}
Type: <class 'str'>



CC BY-NC-ND 2.0 授权