Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在需要python dict的函数之后将对象转换为json字符串_Python_Json - Fatal编程技术网

在需要python dict的函数之后将对象转换为json字符串

在需要python dict的函数之后将对象转换为json字符串,python,json,Python,Json,我有一个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["stu

我有一个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字符串返回?

导入
json
库:

import json
然后使用
json.dumps

return json.dumps(context)
从:

json.dumps(obj,…)

将obj序列化为JSON格式的
str


导入
json
库:

import json
然后使用
json.dumps

return json.dumps(context)
从:

json.dumps(obj,…)

将obj序列化为JSON格式的
str


try
json.dumps(context)
try
json.dumps(context)
I get StudentProfileView
不是json可序列化的
:\@Modelesq如何
返回json.dumps(dict(context))
?Hmm返回关于其他内容的不同错误。是
dict
字面上的dict,还是我的另一个变量?@Modelesq No,
dict
是Python类型,像函数一样调用它会尝试将参数转换为
dict
类型,很像
int
.kk。我想你回答了我的问题。更多的错误倾向于。谢谢:DI get StudentProfileView
不是JSON可序列化的
:\@Modelesq如何
返回JSON.dumps(dict(context))
?Hmm返回关于其他内容的不同错误。是
dict
字面上的dict,还是我的另一个变量?@Modelesq No,
dict
是Python类型,像函数一样调用它会尝试将参数转换为
dict
类型,很像
int
.kk。我想你回答了我的问题。更多的错误倾向于。谢谢:D