从 typescript/javascript 中的 Json 对象读取属性

约书亚

我正在使用从第三方 API 收到的身份验证令牌。我在下面给出了解码令牌的样本,

{
    "nbf": 1564128888,
    "exp": 1564132488,
    "iss": "http://example.com:5002",
    "aud": "http://example.com:5002/resources",

    "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "[email protected]",
    "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse",    
    "amr": ["custom"]
}

我正在努力阅读 javascript 中的“名称”声明。如何在 javascript 或 typescript 中读取该属性?

菲利普

您可以像这样访问复杂的属性名称:

const name = token["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"]

您也可以将其抽象出来以实现可重用性(例如ClaimTypes在 C# 中

const ClaimTypes = {
  name: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
  // other relevant claims
};

const name = token[ClaimTypes.name];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章