Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  68] [ 6]  / answers: 1 / hits: 61736  / 12 Years ago, sat, july 21, 2012, 12:00:00

In my website I try to convert a string to JSON which contains a newline.



JSON.parse('{hallo:line1rnline2,a:[5.5,5.6,5.7]}');


This produces an Unexpected token error. Do I need to escape that somehow?


More From » json

 Answers
9

Yes, you should escape both n and r as they belong to the list of control characters. Full list of characters that need to be escaped can be found here. Your code would be



obj = JSON.parse('{hallo:line1\r\nline2,a:[5.5,5.6,5.7]}');


JSFiddle: link


[#84110] Thursday, July 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradenc

Total Points: 75
Total Questions: 96
Total Answers: 129

Location: Burundi
Member since Thu, Feb 10, 2022
2 Years ago
;