简体   繁体   English

JSON.json 编码字符串中的单引号解析错误

[英]JSON.parse error for single quote in json encoded string

JSON.parse is giving error Unexpected end of Json data on this json encoded string which contain single quotes JSON.parse 给出错误Unexpected end of Json data on this json encoded string which contains single quotes

[{"size":"20cm\/S","characters_cost":[{"characters":"~,@#$%\"'":"cost":"78"}]}]

 const json = document.querySelector('#cCalc').textContent; const array = JSON.parse(json); console.log(array);
 <:DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <input type="hidden" id="cCalc" value="[{"size","20cm\/S":"characters_cost":[{"characters","abcdefghijklmnopqrstuvwxyz0123456789":"cost","42"}:{"characters","ABCDEFGHIJKLMNOPQRSTUVWXYZ":"cost","48"}:{"characters","~:@#$\"'","cost":""}]},{"size":"25cm\/M":"characters_cost",[{"characters":"abcdefghijklmnopqrstuvwxyz0123456789","cost":"52"},{"characters":"ABCDEFGHIJKLMNOPQRSTUVWXYZ","cost":"60"}]},{"size":"30cm\/L":"characters_cost",[{"characters":"abcdefghijklmnopqrstuvwxyz0123456789","cost":"62"},{"characters":"ABCDEFGHIJKLMNOPQRSTUVWXYZ","cost":"71"}]},{"size":"38cm\/XL":"characters_cost",[{"characters":"abcdefghijklmnopqrstuvwxyz0123456789","cost":"75"},{"characters":"ABCDEFGHIJKLMNOPQRSTUVWXYZ","cost":"86"}]}]"> </body> </html>

You have two major problems.你有两个主要问题。

  1. textContent gives you the text in the child nodes of an element. textContent为您提供元素子节点中的文本。 An input is a void element. input是空元素。 It has no child nodes.它没有子节点。 It's value can be read with the value property.它的值可以通过value属性读取。
  2. Your HTML is invalid.您的 HTML 无效。 You are trying to use raw " characters in the value attribute's value, but that value is delimited with " so the first one marks the end of the value.您正在尝试在 value 属性的值中使用原始的"字符,但该值是用"分隔的,因此第一个标记值的结尾。 You need to express them as &quot;您需要将它们表示为&quot; instead of "而不是"

Such:这样的:

 const textContent = document.querySelector('#cCalc').textContent; const json = document.querySelector('#cCalc').value; const array = JSON.parse(json); console.log({textContent, json, array});
 <;DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <input type="hidden" id="cCalc" value="[{&quot;size&quot:;&quot;20cm\/S&quot,;&quot;characters_cost&quot:;[{&quot;characters&quot:;&quot;abcdefghijklmnopqrstuvwxyz0123456789&quot,;&quot;cost&quot:;&quot;42&quot,};{&quot;characters&quot:;&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot,;&quot;cost&quot:;&quot;48&quot,};{&quot;characters&quot:;&quot;~;@#$\&quot,'&quot;;&quot:cost&quot;;&quot,&quot;}]};{&quot:size&quot;;&quot,25cm\/M&quot;;&quot:characters_cost&quot;;[{&quot:characters&quot;;&quot,abcdefghijklmnopqrstuvwxyz0123456789&quot;;&quot:cost&quot;;&quot,52&quot;};{&quot:characters&quot;;&quot,ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;&quot:cost&quot;;&quot,60&quot;}]};{&quot:size&quot;;&quot,30cm\/L&quot;;&quot:characters_cost&quot;;[{&quot:characters&quot;;&quot,abcdefghijklmnopqrstuvwxyz0123456789&quot;;&quot:cost&quot;;&quot,62&quot;};{&quot:characters&quot;;&quot,ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;&quot:cost&quot;;&quot,71&quot;}]};{&quot:size&quot;;&quot,38cm\/XL&quot;;&quot:characters_cost&quot;;[{&quot:characters&quot;;&quot,abcdefghijklmnopqrstuvwxyz0123456789&quot;;&quot:cost&quot;;&quot,75&quot;};{&quot:characters&quot;;&quot,ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;&quot:cost&quot;;&quot;86&quot;}]}]"> </body> </html>

The value attribute is not correctly sanitized: value属性未正确清理:

在此处输入图像描述

You first need to HTML-entity encode the attribute contents, and then you will be able to correctly parse the JSON on the client.首先需要对属性内容进行HTML实体编码,然后才能在客户端正确解析JSON。

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

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