删除渲染模板中的 unicode 字符串
我试图在这个地址下返回一个类似 json 的对象:
http://ntt.vipserv.org/data /shows
但结果我得到:
{'1': {'url': u'http://www.rte.ie/tv/crimecall/', 'image': u'http ://img.rasset.ie/0002c8d0-250.jpg', 'id': u'2', 'name': u'Crimecall'}}
如何摆脱 unicode 字符串?
我的代码:
objects = Show.objects.all()
i = 0
dict = {}
small_dict = {}
for o in objects:
small_dict = {'id': o.id.decode('ascii'), 'url': o.url.decode('ascii'), 'name': o.name.decode('ascii'), 'image': o.image.decode('ascii')}
dict[str(i+1)] = small_dict
small_dict = {}
I'm trying to return a json like object under this address:
http://ntt.vipserv.org/data/shows
but as a result I'm getting :
{'1': {'url': u'http://www.rte.ie/tv/crimecall/', 'image': u'http://img.rasset.ie/0002c8d0-250.jpg', 'id': u'2', 'name': u'Crimecall'}}
How to get rid fo the unicode strings ?
My code:
objects = Show.objects.all()
i = 0
dict = {}
small_dict = {}
for o in objects:
small_dict = {'id': o.id.decode('ascii'), 'url': o.url.decode('ascii'), 'name': o.name.decode('ascii'), 'image': o.image.decode('ascii')}
dict[str(i+1)] = small_dict
small_dict = {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 json 模块,而不是尝试自己编写 JSON 编码器。这将正确格式化双引号中的字符串,并且字符串前面没有
u
。I'd suggest using the json module instead of trying to write a JSON encoder yourself. This will correctly format the strings in double quotes and without the
u
in front of the string.