我抓取一个字符串并使用$ .parseJSON(字符串),但一直收到以下错误:
uncaught exception: Invalid JSON: {"0":{"chartHtml":"<p> Slide 1</p> ","presNote":"","chartOrderIndex":0},"1":{"chartHtml":"<p> Slide 2</p> ","presNote":"","chartOrderIndex":1},"2":{"chartOrderIndex":2},"3":{"chartHtml":"<p> e 3</p> ","presNote":"","chartOrderIndex":3},"4":{"chartHtml":"<p> Slide 4</p> ","presNote":"","chartOrderIndex":4},"5":{"chartHtml":"<p> Slide 5</p> ","presNote":"","chartOrderIndex":5},"6":{"chartOrderIndex":6}}
运行slideJson.d后调试期间的Firebug结果:
>>> slideJson.d
"{"0":{"chartHtml":"<p> Slide 1</p> ","presNote":"","chartOrderIndex":0},"1":{"chartHtml":"<p> Slide 2</p> ","presNote":"","chartOrderIndex":1},"2":{"chartOrderIndex":2},"3":{"chartHtml":"<p> e 3</p> ","presNote":"","chartOrderIndex":3},"4":{"chartHtml":"<p> Slide 4</p> ","presNote":"","chartOrderIndex":4},"5":{"chartHtml":"<p> Slide 5</p> ","presNote":"","chartOrderIndex":5},"6":{"chartOrderIndex":6}}"
以下是对json进行反序列化的一些调用:
function (slideJson) {
var slideObj, jsonStr;
if (slideJson.d) {
slideObj = $.parseJSON(slideJson.d);
Ajax调用以获取具有内容类型的Json:
$.ajax({
url: 'Someurl',
type: "POST",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) { somenamespace.loadSlides(data); }
});
答案 0 :(得分:6)
您的JSON有效,请查看http://jsonlint.com/
您的内容类型如何?如果您使用的是PHP,请设置以下标题:
header('Content-type: application/json');
答案 1 :(得分:2)
有问题的JSON字符串最终在\n
属性中包含\r
和chartHTML
,默认情况下由CKeditor .getData()
方法生成。因为我从萤火虫那里粘贴错误,所以它们不可见,JSON似乎是有效的,这导致了我的困惑。
感谢那些回答。
答案 2 :(得分:1)
你不必使用$.parseJSON
它是形象很好的。尝试直接使用它。
工作demo