admin管理员组文章数量:1313925
I am using the following JS code to parse a JSON string from a separate JS file:
// extract JSON from a module's JS
var jsonMatch = data.match( /\/\*JSON\[\*\/([\s\S]*?)\/\*\]JSON\*\// );
data = JSON.parse( jsonMatch ? jsonMatch[1] : data );
This is an example of the JS file I extract the JSON string from:
JsonString = /*JSON[*/{"entities":[{"type":"EntityPlayer","x":88,"y":138}]}/*]JSON*/;
This code works just fine, however if the JS file with the JSON string contains carriage returns and isn't on one plete line then I get a syntax error.
Example:
JsonString = /*JSON[*/{
"entities":[{
"type":"EntityPlayer",
"x":88,
"y":138}]
}/*]JSON*/;
Returns the following error:
JSON.parse: unexpected non-whitespace character after JSON data
Any idea how I could modify my parsing to work by either stripping out whitespace or to remove carriage returns and new line spaces?
I am using the following JS code to parse a JSON string from a separate JS file:
// extract JSON from a module's JS
var jsonMatch = data.match( /\/\*JSON\[\*\/([\s\S]*?)\/\*\]JSON\*\// );
data = JSON.parse( jsonMatch ? jsonMatch[1] : data );
This is an example of the JS file I extract the JSON string from:
JsonString = /*JSON[*/{"entities":[{"type":"EntityPlayer","x":88,"y":138}]}/*]JSON*/;
This code works just fine, however if the JS file with the JSON string contains carriage returns and isn't on one plete line then I get a syntax error.
Example:
JsonString = /*JSON[*/{
"entities":[{
"type":"EntityPlayer",
"x":88,
"y":138}]
}/*]JSON*/;
Returns the following error:
JSON.parse: unexpected non-whitespace character after JSON data
Any idea how I could modify my parsing to work by either stripping out whitespace or to remove carriage returns and new line spaces?
Share Improve this question asked Jun 14, 2012 at 6:40 gotnullgotnull 27.2k22 gold badges143 silver badges205 bronze badges 3- You could be overthinking it. How are you fetching this file? How do you get it's contents? – Joseph Commented Jun 14, 2012 at 6:46
- I am not seeing that problem: jsfiddle/UrQ7Y – Ray Toal Commented Jun 14, 2012 at 6:50
- I agree with above probably best to try and get rid of the carriage returns and white space rather than code around it. In php you could use some like json_encode to do this for you. – matpol Commented Jun 14, 2012 at 6:52
2 Answers
Reset to default 6data = JSON.parse( (jsonMatch ? jsonMatch[1] : data).replace(/\n/g,"") );
Did not test it, but maybe this is what you are looking for?
var JsonString = JsonString.replace(new RegExp( "\\n", "g" ),"");
(from http://www.bennadel./blog/161-Ask-Ben-Javascript-Replace-And-Multiple-Lines-Line-Breaks.htm)
本文标签: jqueryJavaScriptParsing JSON with carriage and new line spacesStack Overflow
版权声明:本文标题:jquery - JavaScript - Parsing JSON with carriage and new line spaces - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741960886a2407272.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论