Python 对象转换为 JSON
Python 对象转换为 JSON详细操作教程
以下实例为学习把 Python 对象转换为 JSON 字符串,具体代码如下:
# Filename : example.py
# Author by : www.lidihuo.com
import json
print(json.dumps({"name": "Bill", "age": 63}))
print(json.dumps(["apple", "bananas"]))
print(json.dumps(("apple", "bananas")))
print(json.dumps("hello"))
print(json.dumps(42))
print(json.dumps(31.76))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
执行以上代码输出结果为:
# Filename : example.py
# Author by : www.lidihuo.com
{"name": "Bill", "age": 63}
["apple", "bananas"]
["apple", "bananas"]
"hello"
42
31.76
true
False
null