[英]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.你有两个主要问题。
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
属性读取。"
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 "
您需要将它们表示为"
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="[{"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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.