[英]python json.dumps()’s data is contain \, how to clean it
def getJson(filmList):
film_json = FilmJson()
for video in filmList:
videoName = video.videoName
videoUrl = video.videoUrl
videoTime = video.videoTime
dic = {}
if videoName is None:
break
if videoUrl is None:
dic['videourl'] = ""
else:
dic['videourl'] = videoUrl
if videoTime is None:
dic['videotime'] = ""
else:
dic['videotime'] = videoTime
dic['videoname'] = videoName
film_json.videolist.append(dic)
dict__ = film_json.__dict__
print(dict__)
return dict__
The browser send get request 浏览器发送get请求
the backend print 后端打印
{'filmid': '', 'videolist': [{'videourl': '', 'videotime': '', 'videoname': 'Lifeline'}, {'videourl': '', 'videotime': '', 'videoname': 'Ex Static'}, {'videourl': '', 'videotime': '', 'videoname': 'test'}]}
. 。
@api_view(http_method_names=['GET'])
@permission_classes((permissions.AllowAny,))
def sendFilm(request):
....
myjson = jsonbean.getJson(filmList)
return Response(json.dumps(myjson,ensure_ascii=False))
The postman test get result 邮差测试得到结果
"{\"filmid\": \"\", \"videolist\": [{\"videourl\": \"\", \"videotime\": \"\", \"videoname\": \"Lifeline\"}, {\"videourl\": \"\", \"videotime\": \"\", \"videoname\": \"Ex Static\"}, {\"videourl\": \"\", \"videotime\": \"\", \"videoname\": \"test\"}]}"
how to resolve the problem 如何解决问题
You have double dumped. 你有双重倾销。
You just need to json.dumps() once. 你只需要json.dumps()一次。 This error happens because you've json.dumps() on an object that is already a JSON.
发生此错误的原因是您在已经是JSON的对象上有json.dumps()。
return Response( myjson )
should return a regular object without the \\"
. return Response( myjson )
应该返回没有\\"
的常规对象。
Since myjson
is already a JSON string and not a dictionary object. 由于
myjson
已经是JSON字符串而不是字典对象。
将数据传递给Response时不要执行json.dumps
。
You are encoding a JSON object again to JSON using json.dumps(). 您正在使用json.dumps()将JSON对象再次编码为JSON。 If you use json.loads() again then the \\'s will go away.
如果你再次使用json.loads()那么\\将会消失。
return Response(json.loads(json.dumps(myjson,ensure_ascii=False)))
#OR
return Response(myjson)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.