从Ionic 2中的Json字符串中提取值

时间:2017-07-12 07:38:57

标签: json ionic2

我在我的Ionic 2应用程序中使用OAuth2进行授权,并且解码后的令牌响应(我从BASE64.decode()函数获得)就像下面的(键值表单)。我将它存储在名为' tokendata'类型'任何'。现在我想从这个解码的令牌中提取值。现在,如果我只是做了tokendata.personnelnbr',它就不起作用了。此外,如果我执行了json.parse(tokendata)或json.parse(' tokendata'),请将其存储在另一个变量中,例如myvar'然后尝试访问myVar.personnelnbr'然后它也无法正常工作。请帮助解决方案!

0

我尝试访问' personnelnbr'的方法。字段如下:

{
"client_id":"xxx",
"scope":"user_profile",
"sub":"yyy",
"amr":"external",
"auth_time":1499753830,
"idp":"eso_enterprise",
"upn":"yyy",
"email":"yyy",
"samaccount_name":"yyy",
"peoplekey":"1169",
"personnelnbr":"1108",
"given_name":"Deblina",
"sn":"Dutta Chowdhury",
"exp":1499,
"nbf":1499
}

1 个答案:

答案 0 :(得分:3)

如果您只想提取价值,可以这样做:

let datas = {
            "client_id":"xxx",
            "scope":"user_profile",
            "sub":"yyy",
            "amr":"external",
            "auth_time":1499753830,
            "idp":"eso_enterprise",
            "upn":"yyy",
            "email":"yyy",
            "samaccount_name":"yyy",
            "peoplekey":"1169",
            "personnelnbr":"1108",
            "given_name":"Deblina",
            "sn":"Dutta Chowdhury",
            "exp":1499,
            "nbf":1499
        };
for (let key in datas) {
    console.log(key + " => " + datas[key]);
}