Reputation: 675
I have this string
{"cb":15,"cl":12,"cr":18,"ct":3,"id":"6yIGexevqu9RGM:","ml":{"366":{"bh":96,"bw":112,"o":0},"454":{"bh":93,"bw":88,"o":0}},"oh":500,"os":"42KB","ou":"http://albums.songspk.link/images/cover/55553137088b0humnava.jpg","ow":500,"pt":"Humnava
- Hamari Ahduri Kahani (2015) Download Mp3 Songs
...","rh":"albums.songspk.link","ru":"http://albums.songspk.link/audio-mp3-single-track/humnava_hamar_adhuri_kahani_mp3_song","s":"","th":112,"tu":"https://encrypted-tbn2.gstatic.com/images?q\u003dtbn:ANd9GcTlkE4gq7VQB5vB8-35jIF8YBh5KP14GsquWY1allab1-KnL07cREXXajV0_w","tw":112}
If you copy and paste it to JSONLINT, you will see it shows an error because of the line break. If I remove the line breaks the error gets resolved. Is there any way I can parse the string to JSON in JavaScript?
Upvotes: 2
Views: 4644
Reputation: 675
I found solution to my problem....
http://juristr.com/blog/2010/05/n-will-break-your-json-jquery-wcf/
In this article... there is this function... for my thing i changed "\n" with "" [blank space] as i only want JSON to be parsed.
FUNCTION IN WEBSITE
function escapeNewLineChars(valueToEscape) {
if (valueToEscape != null && valueToEscape != "") {
return valueToEscape.replace(/\n/g, "\\n");
} else {
return valueToEscape;
}
}
WHAT I AM USING NOW
function escapeNewLineChars(valueToEscape) {
if (valueToEscape != null && valueToEscape != "") {
return valueToEscape.replace(/\n/g, " ");
} else {
return valueToEscape;
}
}
and my problem got solved.... i replace "\n" with " " ...
Upvotes: 1
Reputation: 2873
Bare linebreaks can't occur in JSON strings; you have to escape them. Depending on the way the string is encoded, the proper escape sequence could be \n
, \r\n
, or \r
. That last one is very unlikely these days, but it can happen if you might be dealing with files that were created on pre-OSX Macs.
However it happens, you need to go through the string and make sure you've escaped any linebreaks within it, and then put that into the JSON file. Alternatively, you could remove all linebreaks from the string, but this might not be feasible for your application.
Upvotes: 0
Reputation: 153
The JSON specification does not mention literal newline as a "char" as part of string values, so you can't just insert in to strings. You can use the escape sequence \n
instead.
Upvotes: 0