意外令牌:u JSON.parse()问题

时间:2021-07-17 04:53:02

I have read online that the unexpected token u issue can come from using JSON.parse(). On my iPhone 5 there is no problem, but on my Nexus 7 I get this sequence of errors:

我在网上读到,意外的令牌u问题可能来自于使用JSON.parse()。在我的iPhone 5上没有问题,但在我的Nexus 7上,我得到了一系列错误:

意外令牌:u JSON.parse()问题View large

查看大

I realize this is a duplicate, but I am not sure how to solve this for my specific problem. Here is where I implement JSON.parse()

我知道这是重复的,但我不知道如何解决我的具体问题。这里是实现JSON.parse()的地方

 $scope.fav = []; 

if ($scope.fav !== 'undefined') {
   $scope.fav = JSON.parse(localStorage["fav"]);
}

4 个解决方案

#1


17  

Base on your updated question the if condition does not make sense, because you set $scope.fav to [] right before, so it can never be "undefined".

基于您更新的问题,if条件没有意义,因为您设置了$scope。喜欢[]之前,所以它永远不会“没有定义”。

Most likely you want to have your test that way:

很可能你想这样做:

if (typeof localStorage["fav"] !== "undefined") {
  $scope.fav = JSON.parse(localStorage["fav"]);
}

As i don't know if there is a situation where localStorage["fav"] could contain the string "undefined" you probably also need test for this.

因为我不知道是否存在localStorage[“收藏”]可能包含字符串“undefined”的情况,您可能还需要对此进行测试。

if (typeof localStorage["fav"] !== "undefined"
    && localStorage["fav"] !== "undefined") {
  $scope.fav = JSON.parse(localStorage["fav"]);
}

#2


10  

One way to avoid the error (not really fixing it, but at least won't break):

避免错误的一种方法(不是真正修复错误,但至少不会出错):

$scope.fav = JSON.parse(localStorage["fav"] || '[]');

You're getting that error because localStorage["fav"] is undefined.

您会得到这个错误,因为localStorage[“收藏”]没有定义。

Try this and you'll understand all by yourself:

试试这个,你自己就会明白:

var a = undefined;
JSON.parse(a);

#3


4  

Unexpected token: u almost always stems from trying to parse a value that is undefined.

意外令牌:u几乎总是源于试图解析未定义的值。

You can guard against that like this:

你可以这样防范:

if (localStorage['fav']) {
  $scope.fav = JSON.parse(localStorage['fav'];
}

#4


0  

In my case, the problem was I was getting the value as localStorage.getItem[key] whereas it should have been localStorage.getItem(key).

在我的例子中,问题是我得到的值是localStorage。getItem[key]而它应该是localStorage.getItem(key)。

The rest and normally faced issues have been better explained already by the above answers.

剩下的和通常面临的问题已经通过上面的答案更好地解释了。

#1


17  

Base on your updated question the if condition does not make sense, because you set $scope.fav to [] right before, so it can never be "undefined".

基于您更新的问题,if条件没有意义,因为您设置了$scope。喜欢[]之前,所以它永远不会“没有定义”。

Most likely you want to have your test that way:

很可能你想这样做:

if (typeof localStorage["fav"] !== "undefined") {
  $scope.fav = JSON.parse(localStorage["fav"]);
}

As i don't know if there is a situation where localStorage["fav"] could contain the string "undefined" you probably also need test for this.

因为我不知道是否存在localStorage[“收藏”]可能包含字符串“undefined”的情况,您可能还需要对此进行测试。

if (typeof localStorage["fav"] !== "undefined"
    && localStorage["fav"] !== "undefined") {
  $scope.fav = JSON.parse(localStorage["fav"]);
}

#2


10  

One way to avoid the error (not really fixing it, but at least won't break):

避免错误的一种方法(不是真正修复错误,但至少不会出错):

$scope.fav = JSON.parse(localStorage["fav"] || '[]');

You're getting that error because localStorage["fav"] is undefined.

您会得到这个错误,因为localStorage[“收藏”]没有定义。

Try this and you'll understand all by yourself:

试试这个,你自己就会明白:

var a = undefined;
JSON.parse(a);

#3


4  

Unexpected token: u almost always stems from trying to parse a value that is undefined.

意外令牌:u几乎总是源于试图解析未定义的值。

You can guard against that like this:

你可以这样防范:

if (localStorage['fav']) {
  $scope.fav = JSON.parse(localStorage['fav'];
}

#4


0  

In my case, the problem was I was getting the value as localStorage.getItem[key] whereas it should have been localStorage.getItem(key).

在我的例子中,问题是我得到的值是localStorage。getItem[key]而它应该是localStorage.getItem(key)。

The rest and normally faced issues have been better explained already by the above answers.

剩下的和通常面临的问题已经通过上面的答案更好地解释了。