JSON.Parse,'未捕获的SyntaxError:意外的令牌o [英] JSON.Parse,'Uncaught SyntaxError: Unexpected token o

查看:284
本文介绍了JSON.Parse,'未捕获的SyntaxError:意外的令牌o的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了从Web服务返回的JSON的问题。看起来JSON缺少引号,但是当我向JSON添加引号时,我收到错误。以下是错误消息:'Uncaught SyntaxError:Unexpected token o。当我将字符串记录到控制台时:[object Object],[object Object]

I am having trouble with JSON returned from a web service. It looks like the JSON lacks quotes, but when I add quotes to the JSON, I get an error. Here is the error message: 'Uncaught SyntaxError: Unexpected token o. When I log the string to console:[object Object],[object Object]

下面是一些模拟错误的示例代码:

Here is some example code that simulates the error:

//Error I am trying to solve
var jsonString = '[{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}]';
var myData = JSON.parse(jsonString);

$(document).ready(function() {
    var $grouplist = $('#groups');
    $.each(myData, function() {
        $('<li>' + this.Name + '</li>').appendTo($grouplist);
    });
});

这是与字符串周围的单引号相同的代码。它的工作原理

Here is the same code with the single quotes around the string. It works

//Successful Javascript
var jsonString = '[{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}]';
var myData = JSON.parse(jsonString);

$(document).ready(function() {
    var $grouplist = $('#groups');
    $.each(myData, function() {
        $('<li>' + this.Name + '</li>').appendTo($grouplist);
    });
});

//Successful HTML
<ul id="groups"></ul>

但是当我尝试在字符串中添加引号时,就像我似乎需要在我的实际代码中,它失败了:

But when I try to add quotes to the string, like I seem to need to in my real code, it fails:

//Does not work when I need to append quotes to the string:
var jsonStringNoQuotes = [{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}];
jsonStringQuotes = "'" + jsonStringNoQuotes + "'";
var myData = JSON.parse(jsonStringQuotes);

$(document).ready(function() {
    var $grouplist = $('#groups');
    $.each(myData, function() {
        $('<li>' + this.Name + ',' +  this.Id + '</li>').appendTo($grouplist);
    });
});

这是错误:
日志字符串到控制台:[object Object],[object对象]
data.js:809 Uncaught SyntaxError:意外的标记'

Here is the error: log string to console:[object Object],[object Object] data.js:809 Uncaught SyntaxError: Unexpected token '

我很难过。任何帮助赞赏!谢谢!

I'm stumped. Any help appreciated! Thank you!

推荐答案

如果没有单引号,你就会创建一个内部有两个对象的数组。这是JavaScript自己的语法。添加引号时,该对象(数组+2个对象)现在是一个字符串。您可以使用 JSON.parse 将字符串转换为JavaScript对象。您不能使用 JSON.parse 将JavaScript对象转换为JavaScript对象。

Without single quotes around it, you are creating an array with two objects inside of it. This is JavaScript's own syntax. When you add the quotes, that object (array+2 objects) is now a string. You can use JSON.parse to convert a string into a JavaScript object. You cannot use JSON.parse to convert a JavaScript object into a JavaScript object.

//String - you can use JSON.parse on it
var jsonStringNoQuotes = '[{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}]';

//Already a javascript object - you cannot use JSON.parse on it
var jsonStringNoQuotes = [{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}];

此外,您的上一个示例失败,因为您要向JSON字符串添加文字单引号字符。这是非法的。 JSON规范声明只允许双引号。如果您要 console.log 以下......

Furthermore, your last example fails because you are adding literal single quote characters to the JSON string. This is illegal. JSON specification states that only double quotes are allowed. If you were to console.log the following...

console.log("'"+[{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}]+"'");
//Logs:
'[object Object],[object Object]'

你会看到它返回数组的字符串表示形式,它被转换为逗号分隔列表,每个列表项都是对象的字符串表示形式,即 [object Object] 。请记住,javascript中的关联数组只是对象,每个键/值对都是属性/值。

You would see that it returns the string representation of the array, which gets converted to a comma separated list, and each list item would be the string representation of an object, which is [object Object]. Remember, associative arrays in javascript are simply objects with each key/value pair being a property/value.

为什么会发生这种情况?因为你是以字符串'开头的,所以你试图将数组附加到它,它请求它的字符串表示,然后你附加另一个字符串'

Why does this happen? Because you are starting with a string "'", then you are trying to append the array to it, which requests the string representation of it, then you are appending another string "'".

请不要将JSON与Javascript混淆,因为它们完全不同。 JSON是一种人类可读的数据格式,旨在匹配创建javascript对象时使用的语法。 JSON是一个字符串。 Javascript对象不是,因此在代码中声明时不会用引号括起来。

Please do not confuse JSON with Javascript, as they are entirely different things. JSON is a data format that is humanly readable, and was intended to match the syntax used when creating javascript objects. JSON is a string. Javascript objects are not, and therefor when declared in code are not surrounded in quotes.

看到这个小提琴:
http://jsfiddle.net/NrnK5/

这篇关于JSON.Parse,'未捕获的SyntaxError:意外的令牌o的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆