简体   繁体   English

JSON 从 python json.dumps 到带有转义 \\" 的 javascript JSON.parse()

[英]JSON from python json.dumps to javascript JSON.parse() with escaped \"

I have been fighting with this problem for a few weeks now and still cannot understand what's wrong here.我已经与这个问题斗争了几个星期,但仍然无法理解这里出了什么问题。 I created a dictionary in python dic .我在 python dic创建了一个字典。 Then I am using dumps to convert it to a valid json.然后我使用转储将其转换为有效的 json。

json_js = json.dumps(dic) # works fine, a valid json from the python's viewpoint
# the reverse operation works fine also
dic = json.loads(json_js)
print(json_js)
==============
{"p0": {"pf": {"id": "pf1", "class": ["pf", "w0", "h0"], "data-page-no": "1"}, "pc": {"class": ["pc", "pc1", "w0", "h0"]}, "img": ["<img alt=\"\" clas

This json_js I use later on to add it to a js script that applies JSON.parse() on it.我稍后使用这个 json_js 将它添加到在其上应用JSON.parse()的 js 脚本。 And this is the error that I get.这是我得到的错误。

SyntaxError: JSON.parse: expected ',' or ']' after array element at line 1 column 143 of the JSON data

That 143 character is the first \\" . But why can't js figure it as a valid JSON I cannot comprehend. Would you have any suggestions what might have gone wrong here?那个 143 个字符是第一个\\" 。但是为什么 js 不能将它视为我无法理解的有效 JSON。您有什么建议吗?这里可能出了什么问题?

EDIT.编辑。 No idea why people close my question.不知道为什么人们会关闭我的问题。 The desired behaviour is that JSON.parse doesn't throw any errors.期望的行为是JSON.parse不会抛出任何错误。 The way how I added it to a script is irrelevant for the question.我如何将它添加到脚本中的方式与问题无关。 Please, have a look at the part of the source code inside html .请看一下 html 中的源代码部分。

const str = `{"p0": {"pf": {"id": "pf1f", "class": ["pf", "w2", "h2"], "data-page-no": "1f"}, "pc": {"class": ["pc", "pc1f", "w2", "h2"]}, "img": ["<img alt=\"\" class=\"bi x0 y0 w1 h1\"` // this string is several megabytes so I only put the first 150 or so characters here.
var dic = JSON.parse(str);

EDIT 2. The full transformation.编辑 2. 完整的转换。

# in python using BeautifulSoup for scripts and new_html
new_html = bs()
dom = bs()
scripts = [dom.new_tag('script')]
scripts[0].string = html_script(json_js) # html_script is a "string... %s" %json_js
new_html.body.append(scripts[0])
with open('stuff.html','w',encoding='utf-8') as f: 
        f.write(str(new_html))

In order to put a string of JSON into JavaScript source code, you used backticks to make it a template string.为了将一串 JSON 放入 JavaScript 源代码,您使用了反引号使其成为模板字符串。 That's not enough, though, since many sequences have special meaning inside backticks:然而,这还不够,因为许多序列在反引号内具有特殊含义:

  • ${…} interpolation ${…}插值
  • backslash escape sequences (the current problem, which converted the \\" in the JSON to " )反斜杠转义序列(当前问题,将 JSON 中的\\"转换为"
  • backticks反引号

Embedding literals in JavaScript in HTML <script> s isn't really trivial, but you can do it by JSON-encoding the JSON, which results in a string that's almost ready to embed except for:在 HTML <script> s 中嵌入 JavaScript 中的文字并不是很简单,但是您可以通过对 JSON 进行 JSON 编码来实现,这会产生一个几乎可以嵌入的字符串,除了:

  • exiting the script element with </script> i使用</script> i退出脚本元素
  • changing the parsing mode with <!--使用<!--更改解析模式
  • creating a poorly-formed JavaScript string literal (for now) with U+2028 or U+2029使用 U+2028 或 U+2029 创建格式不佳的 JavaScript 字符串文字(目前)

which can be avoided by escaping all < s, U+2028, and U+2029.这可以通过转义所有< s、U+2028 和 U+2029 来避免。

In all:在所有:

def script_embeddable_json(value):
    return (
        json.dumps(json.dumps(value))
        .replace("<", "\\u003c")
        .replace("\u2028", "\\u2028")
        .replace("\u2029", "\\u2029"))


json_js = script_embeddable_json(dic)

Then the template should look like const str = %s .然后模板应该看起来像const str = %s

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

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