Purus
Purus

Reputation: 5829

bad control character error in json parse

When parsing a JSON object I am getting "bad control character" error in the Firebug console. There are lot of questions and solutions in this site; but I am unable to crack this issue. I believe I am doing something silly. Please point me to my mistake.

JS Fiddle: http://jsfiddle.net/Purus/Eqz2r/

If we change the json data to a plain text instead of html tags, it works.

Error:

SyntaxError: JSON.parse: bad control character in string literal
someVal = JSON.parse(sign);

NOTE:

Below is the code I have used.

$(document).ready(function () {
   var sign = '{"data":"<br\/><br\/>----------<br \/>\r\nFrom Yahoo Team<br \/>\r\n<a href=\"http:\/\/localhost\/base\/1-yahoo-logo.jpg\" target=\"_blank\"><img style=\"padding: 5px;\" src=\"http:\/\/localhost\/plugins\/1-yahoo-logo.jpg\" height=\"120\" width=\"196\" \/><\/a>"}';

   someVal = JSON.parse(sign);
   $(".demo").append(someVal.data);
});

Upvotes: 2

Views: 18387

Answers (3)

KNC
KNC

Reputation: 104

var s= JsonString;
$.parseJSON(s.replace(/\s+/g,""));

Upvotes: 4

Howard Renollet
Howard Renollet

Reputation: 4739

The problem is the \r and \n. These need to be escaped as

\\r 

and

\\n 

in the JSON string

Upvotes: 2

TimCodes.NET
TimCodes.NET

Reputation: 4689

You need to escape your escapes :)

Use double \\ instead of \

http://jsfiddle.net/Eqz2r/2/

Upvotes: 9

Related Questions