JSON.parse 未捕获的 SyntaxError:意外的令牌 o

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18972167/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:49:57  来源:igfitidea点击:

JSON.parse Uncaught SyntaxError: Unexpected token o

javascriptjson

提问by titi

I have this JSON:

我有这个JSON

var data =  [{
    "ID":1,"Name":"Test",
    "subitem": [
        {"idenID":1,"Code":"254630"},
        {"idenID":2,"Code":"4566"},
        {"idenID":3,"Code":"4566"}
    ]
}];

console.log(JSON.parse(data)); //Uncaught SyntaxError: Unexpected token o 

How to de-serialize datato javascript object.

如何反序列化为datajavascript 对象。

回答by xandercoded

It already is an object ... of type Array. To access the Object:

它已经是一个对象...类型Array。要访问Object

var foo = data[0];

alert(foo.ID);

JSON.parsetakes a Stringand parses it into an equivalent JavaScript value.

JSON.parse接受 aString并将其解析为等效的 JavaScript 值。

回答by Jhankar Mahbub

This is usable in Javascript. You need to parse JSON when your data is in String format and you get it from server side.

这在 Javascript 中可用。当您的数据为字符串格式并从服务器端获取时,您需要解析 JSON。

The purpose of JSON.parse is to convert to Javascipt Object Notation to use it. For example,

JSON.parse 的目的是转换成 Javascipt Object Notation 来使用。例如,

var str = "{"a":1,"b":2}";
var obj = JSON.parse(str); //obj = {a:1, b:2}

Reference MDN

参考MDN