最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON.parse fails on space inside array - Stack Overflow

programmeradmin0浏览0评论

I have saved this string in javascript

js_str= '{"id":"1","user_id":"1","cat_id":"1","name_bz":"Chitwan National Park","name_cf":"Gokarna","cf_lattitude":"27.525","cf_longitude":"87.56","boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\n","area":"989.948","forest_conditon":"Poor","natural_regeneration":"High","grazing_pressure":"Medium","forest_type":"Natural","wild_species_list":"Tiger, Leopard, Rhino","others":null}';

On using js_arr=JSON.parse(js_str) it gives

SyntaxError: JSON.parse: bad control character in string literal at line 1 column 207 of the JSON data

column 207 is the space after ma [27.3564, 85.3564]. The whitespace in this place is giving error. i cannot use regex to remove whitespace as it would replace all whitespaces.

I have saved this string in javascript

js_str= '{"id":"1","user_id":"1","cat_id":"1","name_bz":"Chitwan National Park","name_cf":"Gokarna","cf_lattitude":"27.525","cf_longitude":"87.56","boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\n","area":"989.948","forest_conditon":"Poor","natural_regeneration":"High","grazing_pressure":"Medium","forest_type":"Natural","wild_species_list":"Tiger, Leopard, Rhino","others":null}';

On using js_arr=JSON.parse(js_str) it gives

SyntaxError: JSON.parse: bad control character in string literal at line 1 column 207 of the JSON data

column 207 is the space after ma [27.3564, 85.3564]. The whitespace in this place is giving error. i cannot use regex to remove whitespace as it would replace all whitespaces.

Share Improve this question edited Jan 24, 2016 at 11:37 Bart Friederichs 33.6k16 gold badges111 silver badges218 bronze badges asked Jan 24, 2016 at 11:21 neogeomatneogeomat 3593 silver badges13 bronze badges 2
  • Column 207 is the space. What could be the control character. – neogeomat Commented Jan 24, 2016 at 11:25
  • How are you getting the JSON string from server? Add that code also. – Tushar Commented Jan 24, 2016 at 11:30
Add a ment  | 

2 Answers 2

Reset to default 4

There is a \n in the wrong place or should be escaped. Try to change from:

"boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\n"

to:

"boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]"

or escape it to:

"boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\\n"

Just add a \ before \n control character and all will be fine. JSON parser works like this.

js_str= '{"id":"1","user_id":"1","cat_id":"1","name_bz":"Chitwan National Park","name_cf":"Gokarna","cf_lattitude":"27.525","cf_longitude":"87.56","boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\\n","area":"989.948","forest_conditon":"Poor","natural_regeneration":"High","grazing_pressure":"Medium","forest_type":"Natural","wild_species_list":"Tiger, Leopard, Rhino","others":null}';

JSON.parse(js_str)

Successfully tested code in Mozilla Firefox.

发布评论

评论列表(0)

  1. 暂无评论