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

时间:2014-04-14 07:14:52

标签: javascript

我有json数据我很难变成字符串。如何对其进行格式化以使其成为字符串?

jsfiddle:http://jsfiddle.net/s97QX/2/

代码:

Jsonvar = {"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 <\/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 jsonstr=JSON.stringify(Jsonvar);
alert(jsonstr); 

2 个答案:

答案 0 :(得分:6)

Invalid JSON

首先使用jsonlint验证您的json。 JSONLINT  然后试试。

答案 1 :(得分:2)

控制台中的此错误说明了这一点: Uncaught SyntaxError: Unexpected token ILLEGAL

你的对象有一个换行符:

Jsonvar = {"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":" <<<- Here
Devin Rose

删除它,该属性将成为有效的字符串,因此您可以对其进行字符串化