我有一个python函数/方法,它接收一个学生并将显示配置文件...我意识到我需要将上下文作为json字符串返回。我该怎么做?
context["student"] = db.query_dict(student_profile_sql.format(student_id=self.kwargs["student_id"])
)[0]
appear(self.request, "Show profile", {
"student_name": context["student"]["first_name...
})
return context // i need to return context as json string how can i do that?
如何将上下文作为json字符串返回?
答案 0 :(得分:3)
导入json
库:
import json
然后使用json.dumps
:
return json.dumps(context)
<强>
json.dumps(obj, ...)
强>将obj序列化为格式为
的JSONstr