一、引入json包
二、# 使用json模塊將序列轉換為json字符串,ensure_ascii表示不考慮asii,encoding表示轉換編碼
# json.dumps進行序列化對中文默認使用ascii編碼
# 想要輸出真正的中文就需要指定ensure_ascii為false
代碼:json.dumps(亂碼的中文變量,ensure_ascii=False,encoding='gbk')
例子:
# coding=utf-8
import json
def functionName(level):
try:
if level < 1:
raise Exception(u'這是一個錯誤', level)
except Exception as e:
print type(e.args)
print json.dumps(e.args,ensure_ascii=False,encoding='gbk')#注意e.args 的內容需要是一個object類型,如果不行需要自己轉換
functionName(0)