简体   繁体   中英

json data extract nested objects from _body - Javascript

I am trying to get value of a token but I get error ERROR TypeError: Cannot read property 'token' of undefined

在此输入图像描述

code

//Below code gives the output shown below with black color text
data.text()

// I am interested in fetching token value which is nested inside success keyword, below code fails to get the token
var abc = data.text();
abc['success'].token
let abc = JSON.parse(data.text());
var abc = (JSON.parse(data._body)).success.token;

Following code is for reading JWT form js

function parseJwt (token) {
            var base64Url = token.split('.')[1];
            var base64 = base64Url.replace('-', '+').replace('_', '/');
            return JSON.parse(window.atob(base64));
        };

How to decode jwt token in javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM