从 typescript/javascript 中的 Json 对象读取 属性
Read property from a Json object in typescript/javascript
我正在使用从第三方 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": "Micky@gmail.com",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse",
"amr": ["custom"]
}
我正在努力阅读 javascript 中的 "name" 声明。我如何在 javascript 或 typescript 中读取 属性?
您可以像这样访问复杂的 属性 名称:
const name = token["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"]
您也可以将其抽象出来以实现可重用性(like the ClaimTypes
in C#)
const ClaimTypes = {
name: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
// other relevant claims
};
const name = token[ClaimTypes.name];
JSON.parse(yourData) - 将 JSON 转换为 JS
JSON.stringify(yourData) - 从 JS 到 JSON
所以在 JSON.parse 之后你会得到 JS 对象并且能够得到 yourData.name
在这里您可以阅读:
MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
您将获得字符串形式的数据,将其转换为 json
let jsonData = '{"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": "Micky@gmail.com","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse","amr": ["custom"]}'
let parsedJSON = JSON.parse(jsonData)
console.log(parsedJSON["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"]) // Micky Mouse
console.log(parsedJSON["nbf"]) // 1564128888
console.log(parsedJSON["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"]) // Micky@gmail.com
然后像 parsedJSON["your key"]
一样阅读它。左边的东西是 属性 名称或键。您可以通过
检索它们
let jsonData = '{"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": "Micky@gmail.com","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse","amr": ["custom"]}'
let parsedJSON = JSON.parse(jsonData)
console.log(Object.keys(parsedJSON))
我正在使用从第三方 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": "Micky@gmail.com",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse",
"amr": ["custom"]
}
我正在努力阅读 javascript 中的 "name" 声明。我如何在 javascript 或 typescript 中读取 属性?
您可以像这样访问复杂的 属性 名称:
const name = token["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"]
您也可以将其抽象出来以实现可重用性(like the ClaimTypes
in C#)
const ClaimTypes = {
name: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
// other relevant claims
};
const name = token[ClaimTypes.name];
JSON.parse(yourData) - 将 JSON 转换为 JS JSON.stringify(yourData) - 从 JS 到 JSON
所以在 JSON.parse 之后你会得到 JS 对象并且能够得到 yourData.name
在这里您可以阅读: MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
您将获得字符串形式的数据,将其转换为 json
let jsonData = '{"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": "Micky@gmail.com","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse","amr": ["custom"]}'
let parsedJSON = JSON.parse(jsonData)
console.log(parsedJSON["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"]) // Micky Mouse
console.log(parsedJSON["nbf"]) // 1564128888
console.log(parsedJSON["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"]) // Micky@gmail.com
然后像 parsedJSON["your key"]
一样阅读它。左边的东西是 属性 名称或键。您可以通过
let jsonData = '{"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": "Micky@gmail.com","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse","amr": ["custom"]}'
let parsedJSON = JSON.parse(jsonData)
console.log(Object.keys(parsedJSON))