我在JSON.parse 上遇到语法错误
const userInfo =
localStorage.getItem("user") !== "undefined"
? JSON.parse(localStorage.getItem("user"))
: localStorage.clear();
我在登录组件中首先设置了用户:
const responseGoogle = (response) => {
const decoded = jwt_decode(response.credential);
const anyUser =localStorage.setItem("user", decoded);
这是我的JSON内容:
aud: "57**********-jc601taprt3p68jkkv6pig3ntqk19nt2.apps.googleusercontent.com"
azp: "57**********-jc601taprt3p68jkkv6pig3ntqk19nt2.apps.googleusercontent.com"
email: "user@gmail.com"
email_verified: true
exp: 16676*****
family_name: "John"
given_name: "Doe"
iat: 16676*****
iss: "https://accounts.google.com"
jti: "dbd78b14d4f87f1e32b0ecfc2f8076**********"
name: "John Doe"
nbf: 16676*****
picture: "https://lh3.googleusercontent.com/a/ALm5wu1A2dnuB2PU3zd6Pcc52GGdTWw*****IcMahE_XXw=s96-c"
sub: "1167323005026********"
我使用了jwt_decode,Login组件有setItem,而我的Home组件有getItem
编辑:根据问题更新进行更新。发布的json无效。键需要被引用,值需要逗号——它需要看起来像下面这样:
{
"aud": "57**********-jc601taprt3p68jkkv6pig3ntqk19nt2.apps.googleusercontent.com",
"azp": "57**********-jc601taprt3p68jkkv6pig3ntqk19nt2.apps.googleusercontent.com",
"email": "user@gmail.com",
"email_verified": true,
"exp": 1667600000,
"family_name": "John",
"given_name": "Doe",
"iat": 1667600000,
"iss": "https://accounts.google.com",
"jti": "dbd78b14d4f87f1e32b0ecfc2f8076**********",
"name": "John Doe",
"nbf": 166760000,
"picture": "https://lh3.googleusercontent.com/a/ALm5wu1A2dnuB2PU3zd6Pcc52GGdTW,w*****IcMahE_XXw=s96-c",
"sub": 11673230050260000
}
这是一个很好的资源来帮助json——它可能有点棘手,但一旦你把它放下,它就会变得更有意义。https://www.w3schools.com/js/js_json_datatypes.asp
尝试使用此工具调整jsonhttps://jsonformatter.org/json-parser