简体   繁体   English

如何从simplejson.dumps反序列化(反序列化)JSON中的HTML片段?

[英]How do I deserialise (deserialize) HTML fragments in JSON from simplejson.dumps?

I'm working on a Django project where one of the front end guys has asked that, for one of our ajax request, he would like me to return a JSON dict, with one of the keys being some rendered HTML from a template. 我正在开发一个Django项目,其中一个前端人员曾要求,对于我们的ajax请求之一,他希望我返回JSON dict,其中一个键是从模板中渲染的HTML。 So, eg: 因此,例如:

d = {
  "hits": 22,
  "page": 3,
  "somehhtml": """<div id='item1'>Something</div><div id='item2'>More Stuff</div>

<p>More after some linebreaks.</p>"""
}

When I use simplejson.dumps to return this, it's escaping the HTML, presumably to make it valid JSON, with loads of \\n linebreak characters, and quote chars escaped. 当我使用simplejson.dumps返回此值时,它会转义HTML,大概是使其成为有效的JSON,并带有\\ n换行符,并转义了引号字符。

As a hack I've got it working by adding a string template replacement char to the dict before serialising it with dumps(), and then replacing it with the rendered template before sending it back to the browser. 作为一种技巧,我可以通过在字典上添加一个字符串模板替换字符,然后再使用dumps()对其进行序列化,然后将其替换为呈现的模板,然后再将其发送回浏览器,来使其工作。 This, obviously, is horrible, and surely can't be the correct way to do it. 显然,这是可怕的,而且肯定不是正确的方法。

So, is the correct way to handle this to use some jQuery/JS way of deserialising on the client, or do I need to write my own serialiser at the Python end to not escape certain things? 那么,在客户端上使用某种反序列化的jQuery / JS方法的正确方法是解决此问题,还是我需要在Python端编写自己的序列化程序以免发生某些事情? I tried passing it to eval() in JS, but that gives me a SyntaxError. 我尝试将其传递给JS中的eval(),但这给了我SyntaxError。

Are there practical reasons why what I'm currently doing (ie, not escaping the chars) is a bad idea? 有什么实际的原因使我当前正在做的事情(即,不逃避字符)是个坏主意? (To help me explain, not because I don't think I should change it). (为了帮助我解释,不是因为我不认为应该更改它)。

Thanks! 谢谢!

Ludo. 鲁道

I don't understand why you need to unescape your json string, while it should be escaped to be a valid javascript and python string ? 我不明白为什么您需要取消转义json字符串,而应该将其转义为有效的javascript和python字符串?

And by the way the newlines \\n don't have nothing to do with json dumper is just because the value of the key "somehhtml" is a multiline string that contain new lines (which i think is a mistake maybe you wanted to put <br /> ). 顺便说一下,换行符\\n与json dumper无关,只是因为键“ somehhtml”的值是包含换行符的多行字符串(我认为这是一个错误,也许您想输入<br /> )。

For summary let's put some code :) : 作为总结,我们放一些代码:):

python 蟒蛇

>>> import json
>>> json.dumps(d)
'{"hits": 22, "page": 3, "somehhtml": "<div id=\'item1\'>Something</div><div id=\'item2\'>More Stuff</div>\\n\\n<p>More after some linebreaks.</p>"}'

javascript javascript

>>> s = '{"hits": 22, "page": 3, "somehhtml": "<div id=\'item1\'>Something</div><div id=\'item2\'>More Stuff</div>\\n\\n<p>More after some linebreaks.</p>"}'
>>> JSON.parse(s)
Object { hits=22, page=3, somehhtml="<div id='item1'>Somethi...er some linebreaks.</p>"}
// eval is not safe, but you should add a parentheses as a workaround  
>>> eval('(' + s + ')')
Object { hits=22, page=3, somehhtml="<div id='item1'>Somethi...er some linebreaks.</p>"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM