如何從 Python 函式返回一個 json 物件?


使用給定的 Python 字典,我們可以按照如下方式從 Python 函式返回一個 json 物件。

示例

import json
a = {'name':'Sarah', 'age': 24, 'isEmployed': True }
# a python dictionary
def retjson():
python2json = json.dumps(a)
print python2json
retjson()

輸出

{"age": 24, "isEmployed": true, "name": "Sarah"}

更新於: 13-Feb-2020

2K+ 瀏覽量

開啟您的職業生涯

透過完成學習課程獲得認證

開始學習
廣告