json数据从_body中提取嵌套对象 - Javascript

时间:2017-10-22 12:48:58

标签: javascript json

我正在尝试获取令牌的价值但我收到错误ERROR TypeError: Cannot read property 'token' of undefined

enter image description here

//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

3 个答案:

答案 0 :(得分:2)

let abc = JSON.parse(data.text());

答案 1 :(得分:1)

var abc = (JSON.parse(data._body)).success.token;

答案 2 :(得分:0)

以下代码用于阅读JWT表单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