在解析有效的JSON字符串时遇到问题

时间:2016-11-04 11:47:09

标签: javascript json parsing

如何解析这个:

JSON.stringify({"a":JSON.stringify({"Max":"2"})});
// prints - {"a":"{\"Max\":\"2\"}"}
JSON.parse(('{"a":"{\"Max\":\"2\"}"}'))
// gives error - unexpected token M

我究竟做错了什么?

更新1: Pranav的解决方案运作良好。但不知何故,我无法解析以下字符串:

var str = '{"a":"{\\"PRINT\\":{\\"Maximum Printing Resolution\\":\\"4800 (horizontal)*1 x 600 (vertical) dpi\\",\\"Recommended Printing Area\\":\\"Top margin: 31.6mm     Bottom margin: 29.2mm\\",\\"Paper Size\\":\\"A4\\",\\"Paper Handling\\":\\"A4, A5, B5,\\",\\"Paper Weight\\":\\"Plain Paper: 64-105 g/m2\\",\\"Ink End Sensor\\":\\"Dot count\\",\\"Print Head Alignment\\":\\"Manual\\"},\\"SCAN\\":{\\"Scanner Type\\":\\"Flatbad\\",\\"Scanning Method\\":\\"CIS\\",\\"Optical Resolution\\":\\"600 x 1200dpi\\",\\"Selectable Resolution\\":\\"25 - 19200dpi\\",\\"Scanning Bit Depth (Input/Output)\\":\\"16bit / 8bit\\",\\"Line Scanning Speed\\":\\"1.2ms/line (300dpi)\\",\\"Scanning Speed\\":\\"Approx. 19secs\\",\\"Maximum Document Size\\":\\"A4 / Letter (216 x 297mm)\\",\\"Compatible Media\\":\\"Size:  A4, Letter      Type:   Plain Paper\\",\\"Image Quality\\":\\"Draft, Standard\\"},\\"COPY\\":{\\"Copy Speed\\":\\"Approx. 30secs\\",\\"Multiple Copy\\":\\"1 - 21 pages\\"},\\"General\\":{\\"Brand\\":\\"Canon\\",\\"Model Name\\":\\"PIXMA E410\\",\\"Functionality\\":\\"Multi Function\\"},\\"Box Contents\\":{\\"In the Box\\":\\"Power Cable , USB Cable , Manual book with CD & Cartridge\\"},\\"Dimensions\\":{\\"Dimensions - L (Cms)\\":\\"426\\",\\"Dimensions - B (Cms)\\":\\"306\\",\\"Dimensions - H (Cms)\\":\\"145\\"},\\"Warranty Period\\":{\\"Warranty(Years)\\":\\"1\\"},\\"Additional Features\\":{},\\"Compatible Cartridge\\":{}}"}'

更新2:

确切代码:

var str = "{\"PRINT\":{\"Maximum Printing Resolution\":\"4800 (horizontal)*1 x 600 (vertical) dpi\",\"Recommended Printing Area\":\"Top margin: 31.6mm     Bottom margin: 29.2mm\",\"Paper Size\":\"A4\",\"Paper Handling\":\"A4, A5, B5,\",\"Paper Weight\":\"Plain Paper: 64-105 g/m2\",\"Ink End Sensor\":\"Dot count\",\"Print Head Alignment\":\"Manual\"},\"SCAN\":{\"Scanner Type\":\"Flatbad\",\"Scanning Method\":\"CIS\",\"Optical Resolution\":\"600 x 1200dpi\",\"Selectable Resolution\":\"25 - 19200dpi\",\"Scanning Bit Depth (Input/Output)\":\"16bit / 8bit\",\"Line Scanning Speed\":\"1.2ms/line (300dpi)\",\"Scanning Speed\":\"Approx. 19secs\",\"Maximum Document Size\":\"A4 / Letter (216 x 297mm)\",\"Compatible Media\":\"Size:\tA4, Letter      Type:\tPlain Paper\",\"Image Quality\":\"Draft, Standard\"},\"COPY\":{\"Copy Speed\":\"Approx. 30secs\",\"Multiple Copy\":\"1 - 21 pages\"},\"General\":{\"Brand\":\"Canon\",\"Model Name\":\"PIXMA E410\",\"Functionality\":\"Multi Function\"},\"Box Contents\":{\"In the Box\":\"Power Cable , USB Cable , Manual book with CD & Cartridge\"},\"Dimensions\":{\"Dimensions - L (Cms)\":\"426\",\"Dimensions - B (Cms)\":\"306\",\"Dimensions - H (Cms)\":\"145\"},\"Warranty Period\":{\"Warranty(Years)\":\"1\"},\"Additional Features\":{},\"Compatible Cartridge\":{}}";

str = '{"a":"'+str.replace(/"/g,'\\\"').replace(/\n/g,'')+'"}';

console.log(str);
console.log(JSON.parse(str));

3 个答案:

答案 0 :(得分:2)

您需要使用\来转义\,因为它在字符串中具有特殊含义。

JSON.parse(('{"a":"{\\"Max\\":\\"2\\"}"}'))



console.log(
  JSON.parse(('{"a":"{\\"Max\\":\\"2\\"}"}'))
)




UPDATE:如果您想将字符串设为有效的JSON,请使用JSON.stringify方法。



var str = "{\"PRINT\":{\"Maximum Printing Resolution\":\"4800 (horizontal)*1 x 600 (vertical) dpi\",\"Recommended Printing Area\":\"Top margin: 31.6mm     Bottom margin: 29.2mm\",\"Paper Size\":\"A4\",\"Paper Handling\":\"A4, A5, B5,\",\"Paper Weight\":\"Plain Paper: 64-105 g/m2\",\"Ink End Sensor\":\"Dot count\",\"Print Head Alignment\":\"Manual\"},\"SCAN\":{\"Scanner Type\":\"Flatbad\",\"Scanning Method\":\"CIS\",\"Optical Resolution\":\"600 x 1200dpi\",\"Selectable Resolution\":\"25 - 19200dpi\",\"Scanning Bit Depth (Input/Output)\":\"16bit / 8bit\",\"Line Scanning Speed\":\"1.2ms/line (300dpi)\",\"Scanning Speed\":\"Approx. 19secs\",\"Maximum Document Size\":\"A4 / Letter (216 x 297mm)\",\"Compatible Media\":\"Size:\tA4, Letter      Type:\tPlain Paper\",\"Image Quality\":\"Draft, Standard\"},\"COPY\":{\"Copy Speed\":\"Approx. 30secs\",\"Multiple Copy\":\"1 - 21 pages\"},\"General\":{\"Brand\":\"Canon\",\"Model Name\":\"PIXMA E410\",\"Functionality\":\"Multi Function\"},\"Box Contents\":{\"In the Box\":\"Power Cable , USB Cable , Manual book with CD & Cartridge\"},\"Dimensions\":{\"Dimensions - L (Cms)\":\"426\",\"Dimensions - B (Cms)\":\"306\",\"Dimensions - H (Cms)\":\"145\"},\"Warranty Period\":{\"Warranty(Years)\":\"1\"},\"Additional Features\":{},\"Compatible Cartridge\":{}}";

str = '{"a":'+JSON.stringify(str)+'}';

//console.log(str);
console.log(JSON.parse(str));




答案 1 :(得分:1)

基本上,JSON内有\t。此字符无效。

使用右侧转义标签替换,您可以解析字符串。



var s = "{\"PRINT\":{\"Maximum Printing Resolution\":\"4800 (horizontal)*1 x 600 (vertical) dpi\",\"Recommended Printing Area\":\"Top margin: 31.6mm     Bottom margin: 29.2mm\",\"Paper Size\":\"A4\",\"Paper Handling\":\"A4, A5, B5,\",\"Paper Weight\":\"Plain Paper: 64-105 g/m2\",\"Ink End Sensor\":\"Dot count\",\"Print Head Alignment\":\"Manual\"},\"SCAN\":{\"Scanner Type\":\"Flatbad\",\"Scanning Method\":\"CIS\",\"Optical Resolution\":\"600 x 1200dpi\",\"Selectable Resolution\":\"25 - 19200dpi\",\"Scanning Bit Depth (Input/Output)\":\"16bit / 8bit\",\"Line Scanning Speed\":\"1.2ms/line (300dpi)\",\"Scanning Speed\":\"Approx. 19secs\",\"Maximum Document Size\":\"A4 / Letter (216 x 297mm)\",\"Compatible Media\":\"Size:\tA4, Letter      Type:\tPlain Paper\",\"Image Quality\":\"Draft, Standard\"},\"COPY\":{\"Copy Speed\":\"Approx. 30secs\",\"Multiple Copy\":\"1 - 21 pages\"},\"General\":{\"Brand\":\"Canon\",\"Model Name\":\"PIXMA E410\",\"Functionality\":\"Multi Function\"},\"Box Contents\":{\"In the Box\":\"Power Cable , USB Cable , Manual book with CD & Cartridge\"},\"Dimensions\":{\"Dimensions - L (Cms)\":\"426\",\"Dimensions - B (Cms)\":\"306\",\"Dimensions - H (Cms)\":\"145\"},\"Warranty Period\":{\"Warranty(Years)\":\"1\"},\"Additional Features\":{},\"Compatible Cartridge\":{}}";

s = s.replace(/\t/g, '\\\\t');
console.log(JSON.parse(s));
console.log(s);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 2 :(得分:0)

我认为你误解了JSON的概念以及PARSE和STRINGIFY的使用。当不要解析JSON对象时,你不要字符串化字符串。

在你的例子中,这正是你正在做的事情,你正在字符串化字符串(我想你的意思是PARSE代替)。

所以:

data:image/png;base64,iVBORw0KGgoAAAANSUhE

将JSON对象分配给aaa = JSON.parse({"a":JSON.stringify({"Max":"2"})});

aaa

将打印JSON的字符串版本。