JSON 中的 HTML 标签(Python 中)
我知道这不是一个理想的情况,但是如果我需要在 JSON 标签中包含某种 HTML,例如:
{
"node":
{
"list":"<ul><li class="lists">Hello World</li><ul>"
}
}
这可以在 Python 中完成而不需要事先转义吗?
它最初是一个字符串,所以我正在考虑编写一个正则表达式来尝试在处理之前匹配和转义它们,但我只是想确保没有更简单的方法。
I understand its not a desirable circumstance, however if I NEEDED to have some kind of HTML within JSON tags, e.g.:
{
"node":
{
"list":"<ul><li class="lists">Hello World</li><ul>"
}
}
is this possible to do in Python without requiring to to be escaped beforehand?
It will be a string initially so I was thinking about writing a regular expression to attempt to match and escape these prior to processing, but I just want to make sure there isn't an easier way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,根据您的 HTML 的变化程度,您可以在 HTML 中使用单引号,因此您可以这样做:
但是,使用
simplejson
,它内置于 Python 2.6 中,作为 json 模块,它会自动执行您需要的任何转义:Well, depending on how varied your HTML is, you can use single quotes in HTML fine, so you could do:
However, with
simplejson
, which is built into Python 2.6 as the json module, it does any escaping you need automatically:您可以在那里拥有任意字符串,包括那些碰巧包含 HTML 标记的字符串(您的示例的唯一问题是内部
"
,它会混淆任何解析器)。You can have arbitrary strings there, including ones which happen to contain HTML tags (the only issue with your example is the inner
"
which would confuse any parser).