当其他解析器说它有效时,json.parse会在意外令牌上失败

时间:2015-04-02 22:35:15

标签: javascript jquery json

您好我正在尝试使用

在javascript中将数据转换为json
Json.parse(data);

并继续说

SyntaxError: Unexpected token 

at Object.parse (native)
at new t (eval at <anonymous> (https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js:2:14070), <anonymous>:1:54784)
at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:36:315)
at $get.w.instance (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:75:451)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:58:476
at s (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:7:408)
at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:58:459)
at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:299)
at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:316)
at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:316)

我猜它说某个地方有一个空间,但我在网上使用了一堆解析器工具,它说json是有效的。是否有一个更可靠的Json解析器,我可以在javascript中使用或如何确保我的数据是可分析的

这是我想要转换的字符串

[{"SessionToken":null,"ErrorMessage":null,"BusinessId":0,"CustomerId":0,"Name":null,"EMail":"","ContactId":"","KeyValueTags":null,"AccountId":"","FirstClickId":0,"LastClickId":0,"SFServerName":"","LeadId_Current":null,"Lead_Type":0,"TotalForms":1,"TotalSessions":2,"TotalCustomEvents":0,"TotalABTests":0,"PageViews":5,"LastVisit":"2015-03-29T07:44:17Z","TimeZoneOffset":"16:00:00","SessionsData":[{"WebSource":"Google AdWords","ReferralId":0,"SessionId":0,"UserId":"","Email":"","ContactUrl":null,"SessionDate":"2015-03-29T07:15:14Z","ClickType":"LC","ABTests":[],"CustomEvents":[],"HasForm":false,"Events":[{"EventType":"PageView","EventSource":"","EventTime":"2015-03-29T07:15:14Z","GMT_EventTime":"2015-03-29T07:15:14Z","PageUrl":"http://www.mycase.com/legal-practice-management-software?_bt=42684442540&_bk=%2bmy+%2bcase&_bm=b&gclid=CJf-...eView","EventSource":"","EventTime":"2015-03-29T07:11:30Z","GMT_EventTime":"2015-03-29T07:11:30Z","PageUrl":"http://www.mycase.com/legal-practice-management-software?_bt=42684442540&_bk=%2bmy+%2bcase&_bm=b&gclid=CJf-nJOBzcQCFYRCaQodro4Akw","PageTitle":"MyCase | Legal Practice Management Software","ReferrerUrl":"http://www.googleadservices.com/pagead/aclk?sa=L&ai=C2hcjjaUXVZaGKemLsAf4zIDwBbyXlZpBnIqW4p4B-7vf3p4BCAAQASCjn9UmYMnOq4j0o7QSoAHs1tXTA8gBAaoEKk_QBDZzCtwBsfQY3U_e_qDQKNtnQiD2MOh6BXZjEPh9kHPb2HWDq_TA54AFs_j-GIgGAdgGBIAH_KiqLJAHA6gHpr4b2AcB&ohost=www.google.com&cid=5GhDCu-n2kqN4eYWZsysECDFS4D7Gd5wpxhcethUGY2l-qg&sig=AOD64_2xEQSmG4Lgj7Tux2Hszj5yu5WNZA&rct=j&q=&ved=0CCAQ0Qw&adurl=http://www.mycase.com/legal-practice-management-software%3F_bt%3D42684442540%26_bk%3D%252Bmy%2520%252Bcase%26_bm%3Db"}],"Device":{"BrowserInfo":{"Name":"Chrome","Version":"30.0"},"PlatformInfo":{"Name":"Android","Version":"4.4"}},"IPInfo":null}],"Cookies":[""],"Hosts":null}]

2 个答案:

答案 0 :(得分:1)

你拥有的已经是Array类型的对象。要访问对象:

var foo = data[0];

alert(foo.SessionToken); //for example..

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

答案 1 :(得分:1)

问题是JSON.parse需要JSON对象,而不是JSON数组。解决方法是将数组包装在对象中:

var rawData = '[{"SessionToken":null,"ErrorMessage":null,"BusinessId":0,"CustomerId":0,"Name":null,"EMail":"","ContactId":"","KeyValueTags":null,"AccountId":"","FirstClickId":0,"LastClickId":0,"SFServerName":"","LeadId_Current":null,"Lead_Type":0,"TotalForms":1,"TotalSessions":2,"TotalCustomEvents":0,"TotalABTests":0,"PageViews":5,"LastVisit":"2015-03-29T07:44:17Z","TimeZoneOffset":"16:00:00","SessionsData":[{"WebSource":"Google AdWords","ReferralId":0,"SessionId":0,"UserId":"","Email":"","ContactUrl":null,"SessionDate":"2015-03-29T07:15:14Z","ClickType":"LC","ABTests":[],"CustomEvents":[],"HasForm":false,"Events":[{"EventType":"PageView","EventSource":"","EventTime":"2015-03-29T07:15:14Z","GMT_EventTime":"2015-03-29T07:15:14Z","PageUrl":"http://www.mycase.com/legal-practice-management-software?_bt=42684442540&_bk=%2bmy+%2bcase&_bm=b&gclid=CJf-...eView","EventSource":"","EventTime":"2015-03-29T07:11:30Z","GMT_EventTime":"2015-03-29T07:11:30Z","PageUrl":"http://www.mycase.com/legal-practice-management-software?_bt=42684442540&_bk=%2bmy+%2bcase&_bm=b&gclid=CJf-nJOBzcQCFYRCaQodro4Akw","PageTitle":"MyCase | Legal Practice Management Software","ReferrerUrl":"http://www.googleadservices.com/pagead/aclk?sa=L&ai=C2hcjjaUXVZaGKemLsAf4zIDwBbyXlZpBnIqW4p4B-7vf3p4BCAAQASCjn9UmYMnOq4j0o7QSoAHs1tXTA8gBAaoEKk_QBDZzCtwBsfQY3U_e_qDQKNtnQiD2MOh6BXZjEPh9kHPb2HWDq_TA54AFs_j-GIgGAdgGBIAH_KiqLJAHA6gHpr4b2AcB&ohost=www.google.com&cid=5GhDCu-n2kqN4eYWZsysECDFS4D7Gd5wpxhcethUGY2l-qg&sig=AOD64_2xEQSmG4Lgj7Tux2Hszj5yu5WNZA&rct=j&q=&ved=0CCAQ0Qw&adurl=http://www.mycase.com/legal-practice-management-software%3F_bt%3D42684442540%26_bk%3D%252Bmy%2520%252Bcase%26_bm%3Db"}],"Device":{"BrowserInfo":{"Name":"Chrome","Version":"30.0"},"PlatformInfo":{"Name":"Android","Version":"4.4"}},"IPInfo":null}],"Cookies":[""],"Hosts":null}]';
var parsed = JSON.parse("{ \"data\": " + rawData + " }").data;
console.log(parsed);