为什么我的json无法转换为字符串?

时间:2014-04-14 13:24:46

标签: javascript

我有一个获取远程JSON的函数,但是我认为json的格式很差,所以我不能在它上面使用JSON.stringify方法。我认为这是由于其中的撇号所包含的。我无法访问服务器。有没有办法我可以解析和更改json,以便所有远程json拉出格式正确?

这是现在的格式。

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);

2 个答案:

答案 0 :(得分:3)

错字:

var = jsonstring

将其替换为

var jsonstring

另外,我在您的变量SyntaxError: Unexpected token ILLEGAL中获得jsonst

在您的变量中似乎有一个换行符:

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

D中的Devlin之前移除换行符/换行符,然后jsonstring变量将被填充正常:

"{"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"}]}"

答案 1 :(得分:0)

description键的值是抛出解析错误。没有换行符。而不是"description": " Devin Rose grew up a militant's</p>",只有"description": "Devin Rose grew up a militant's</p>",