简体   繁体   English

Javascript :: JSON解析不接受换行符?

[英]Javascript:: JSON parse does not accept line breaks?

I wrote a sample code just to explain what I was after so... 我写了一个示例代码,只是为了解释我之后的情况......

Here is a working example: 这是一个工作示例:

var json='{"hezi":"firstName","gangina":"lastName"}',
    obj=JSON.parse(json);

alert(obj.gangina);

And here is the same exact code with line breaks and tabs (just to make the code more readable since in my real code the JSON array string is HUGE): 这里有与换行符和制表符完全相同的代码(只是为了使代码更具可读性,因为在我的实际代码中JSON数组字符串是巨大的):

var json=
'
    {
        "hezi":"firstName",
        "gangina":"lastName"
    }
',
    obj=JSON.parse(json);

alert(obj.gangina);

I even tried to compensate with : 我甚至试图补偿:

    obj=JSON.parse(json.replace(/(\r\n|\n|\r|\t)/gm,""));

So... Technically I can solve this issue by compressing my line (removing all \\r\\n|\\n|\\r|\\t manually) but I'm quite sure there is a quick fix for that regardless beautifying my code. 所以...从技术上讲,我可以通过压缩我的行来解决这个问题(手动删除所有\\r\\n|\\n|\\r|\\t ),但我很确定无论如何美化我的代码都有一个快速修复。

Small tweak needed here... 这里需要小的调整......

JavaScript does not accept line breaks without escaping. JavaScript不接受换行而不转义。 You can fix this by escaping the line breaks: 您可以通过转义换行符来解决此问题:

var json=
'\
    {\
        "hezi":"firstName",\
        "gangina":"lastName"\
    }\
',
obj=JSON.parse(json);

alert(obj.gangina);

Just to clear up the confusion. 只是为了消除困惑。 The error is here: 错误在这里:

var json=
'
    {

On line 2, there's a quote: the beginning of a string . 在第2行,有一个引用: 字符串的开头。 Then an unescaped line break. 然后一个未转义的线断裂。 That causes a SyntaxError and none of your code is ever executed. 这会导致语法错误,并且您的代码都没有被执行。 In particular, JSON.parse isn't executed. 特别是,JSON.parse没有被执行。

I suppose you want to 'pretty print' long JSON-strings? 我想你想'打印'长JSON字符串?

In that case: you can use the space parameter in JSON.stringify to display a long JSON-string formatted. 在这种情况下:您可以使用JSON.stringifyspace参数来显示格式化的长JSON字符串。 See MDN . MDN For example: 例如:

 var json = '{"hezi":"firstName","gangina":"lastName"}' obj = JSON.parse(json); document.querySelector('#result').innerHTML = JSON.stringify(obj, null, ' '); 
 <pre id="result"></pre> 

ES6 template strings are now supported in Chrome 41+ and Firefox 34+. Chrome 41+和Firefox 34+现在支持ES6 模板字符串 It's time to know about it. 是时候了解它。

JSON.parse(`
   {
        "hezi":"firstName",
        "gangina":"lastName"
    }
`);

You need to use line breaks to achieve this but escaping is not recommended due to it being easy to escape a space by mistake which would throw a formatting error. 您需要使用换行符来实现此目的,但不推荐转义,因为它很容易错误地逃避空间,这会引发格式错误。

Use.. 采用..

var json=
'{'+
 '"hezi":"firstName",'+
 '"gangina":"lastName"'+
'}',
    obj=JSON.parse(json);

alert(obj.gangina);

But my question would be why use json in the 1st place, if hand coding use an object then this wouldn't be an issue, json data should only be loaded from external source. 但我的问题是为什么在第一个地方使用json,如果手工编码使用一个对象然后这不会是一个问题,json数据应该只从外部源加载。

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

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