Brandon
Brandon

Reputation: 309

Why is my json unable to be converted to a string?

I have a function that gets remote JSON however that json I believe is poorly formatted so I can't use the JSON.stringify method on it. I think it's due to the apostrophe contained in some of it. I have no access to the sever. Is there a way I can parse and change the json so that all remote json pulled is formatted correctly?

this is the format now.

var jsonst = {"shows":[{"show_id":6387, "shownum":6387,"title":"The Protestant's  Dilemma","guest":"Devin Rose","category":"Non-Catholic","url":"http://www.catholic.com /radio/shows/the-protestants-dilemma-11565","audiourl":"http://www.catholic.com/sites /default/files/audio/radioshows/ca140331b.mp3","datetime":"1396317600","description":"
Devin Rose grew up a militant's<\/p>","thumbnailsmall":"http://www.catholic.com/sites/default/files/imagecache/profile_square_small/images/profilepics/a109aad8daa70ad8976ffc.L._V387899120_SX200_.jpg","thumbnaillarge":"http://www.catholic.com/sites/default/files/imagecache/profile_square_large/images/profilepics/a109aad8daa70ad8976ffc.L._V387899120_SX200_.jpg"}]}

var = jsonstring = JSON.stringify(jsonst);
alert(jsonstring);

Upvotes: 0

Views: 38

Answers (2)

Rajesh
Rajesh

Reputation: 3778

The value of the description key is throwing parse error. Dont have the line break in it. Instead of "description": " Devin Rose grew up a militant's</p>", just have "description": "Devin Rose grew up a militant's</p>",

Upvotes: 0

Cerbrus
Cerbrus

Reputation: 72857

Typo:

var = jsonstring

Replace that with

var jsonstring

Also, I'm getting a SyntaxError: Unexpected token ILLEGAL in your variable jsonst:

It seems like there's a newline character in your variable at:

"description":"
Devin Rose grew up ....

Remove the newline / linebreak before the D in Devlin, then the jsonstring variable will be filled just fine:

"{"shows":[{"show_id":6387,"shownum":6387,"title":"The Protestant's  Dilemma","guest":"Devin Rose","category":"Non-Catholic","url":"http://www.catholic.com /radio/shows/the-protestants-dilemma-11565","audiourl":"http://www.catholic.com/sites /default/files/audio/radioshows/ca140331b.mp3","datetime":"1396317600","description":"Devin Rose grew up a militant's</p>","thumbnailsmall":"http://www.catholic.com/sites/default/files/imagecache/profile_square_small/images/profilepics/a109aad8daa70ad8976ffc.L._V387899120_SX200_.jpg","thumbnaillarge":"http://www.catholic.com/sites/default/files/imagecache/profile_square_large/images/profilepics/a109aad8daa70ad8976ffc.L._V387899120_SX200_.jpg"}]}"

Upvotes: 3

Related Questions