gpt4 book ai didi

python json.dumps()的数据包含\,如何清理它

转载 作者:行者123 更新时间:2023-11-30 22:26:16 24 4
gpt4 key购买 nike

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__

浏览器发送get请求

后台打印

{'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))

postman 测试得到结果

"{\"filmid\": \"\", \"videolist\": [{\"videourl\": \"\", \"videotime\": \"\", \"videoname\": \"Lifeline\"}, {\"videourl\": \"\", \"videotime\": \"\", \"videoname\": \"Ex Static\"}, {\"videourl\": \"\", \"videotime\": \"\", \"videoname\": \"test\"}]}"

如何解决问题

最佳答案

你被双重甩了。

你只需要 json.dumps() 一次。发生此错误是因为您对已经是 JSON 的对象使用了 json.dumps()。

return Response( myjson ) 应返回一个不带 \" 的常规对象。

因为 myjson 已经是一个 JSON 字符串而不是字典对象。

关于python json.dumps()的数据包含\,如何清理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47260823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com