javascript JSON.parse:意外的字符错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10001123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON.parse: unexpected character error
提问by RHammonds
I'm having a problem. I have the list of JSON objects in a separate file but want to parse them into a data table. Every time I try to parse them, I get an unexpected character error...
我有问题。我在单独的文件中有 JSON 对象列表,但想将它们解析为数据表。每次我尝试解析它们时,都会遇到一个意想不到的字符错误......
Here is the code
这是代码
var myJSONObject = {
"orders" : [{
"orderId" : "K2_001",
"dueDate" : "04/15/2012",
"priority" : 1,
"description" : "ORDER K2_001"
}, {
"orderId" : "K2_002",
"dueDate" : "04/20/2012",
"priority" : 2,
"description" : "ORDER K2_002"
}, {
"orderId" : "K2_003",
"dueDate" : "04/23/2012",
"priority" : 3,
"description" : "ORDER K2_003"
}, {
"orderId" : "K2_004",
"dueDate" : "04/27/2012",
"priority" : 4,
"description" : "ORDER K2_004"
}, {
"orderId" : "K2_005",
"dueDate" : "04/30/2012",
"priority" : 5,
"description" : "ORDER K2_005"
}, {
"orderId" : "K2_006",
"dueDate" : "05/05/2012",
"priority" : 6,
"description" : "ORDER K2_006"
}, {
"orderId" : "K2_007",
"dueDate" : "05/12/2012",
"priority" : 7,
"description" : "ORDER K2_007"
}, {
"orderId" : "K2_008",
"dueDate" : "05/14/2012",
"priority" : 8,
"description" : "ORDER K2_008"
}]
};
var jsonObject2 = Y.JSON.parse(myJSONObject.responseText);
回答by Rocket Hazmat
JSON is a stringrepresentation of a (JavaScript) object. A JSON string, is a valid JavaScript object.
JSON 是(JavaScript) 对象的字符串表示形式。JSON字符串是有效的 JavaScript对象。
Example:
例子:
var JSON = '{"Hello": "world", "test": [1,2,3]}'; // <= This is JSON, it's a string
var obj = {"Hello": "world", "test": [1,2,3]}; // <= This is a JavaScript object
In your example, myJSONObject
is alreadyan object, it doesn't need to be "parsed".
在你的榜样,myJSONObject
是已经一个对象,它不需要被“解析”。
回答by Vish
This is one problem i had faced and the solution is related to usage of double quotes.
这是我遇到的一个问题,解决方案与双引号的使用有关。
http://mywpf-visu.blogspot.in/2012/04/json-encountered-unexpected-character.html
http://mywpf-visu.blogspot.in/2012/04/json-encountered-unexpected-character.html