如何摆脱Python json.dumps中的“quot” [英] How to get rid of 'quot' in Python json.dumps

查看:104
本文介绍了如何摆脱Python json.dumps中的“quot”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,我有一个视图,它请求JSON提要并将响应与模板一起呈现。我需要将JSON对象转换为Javascript JSON对象。我已经设法在我的模板中控制了.log对象JSON对象,但格式有问题。

In Django, I have a view that requests a JSON feed and renders the response along with a template. I need to convert the JSON object to a Javascript JSON object. I have managed to console.log the JSON object in my template, but something is wrong with the format.

这就是我的期望:

{"lat": 58.548703, "referenceTime": "2013-12-05T07:00:00Z", "lon": 16.155116...

这就是我得到的:

{"lat": 58.548703, "referenceTime": "2013-12-05T07:00:00Z", "lon": 16.155116...

这是我的观点:

def myView(request):
    request = Request('http://somedomain/somefeed.json')

    response = urlopen(request)
    data = json.load(response)
    return render_to_response('myTemplate.html', {'myJson': json.dumps(data)})

在我的模板中:

console.log('{{myJson}}');


推荐答案

正如Armance在评论中所说,你需要标记该变量在模板中是安全的,因此不会被转义:

As Armance says in the comments, you need to mark the variable as safe in the template so it is not escaped:

{{ myJson|safe }}

这篇关于如何摆脱Python json.dumps中的“quot”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆