我有一个名为 UserService 的服务,它返回登录用户的角色。
它返回[{"authority":"ROLE_APEX_SUPPORT"},{"authority":"ROLE_APEX_READONLY"}]
或[{"authority":"ROLE_APEX_READONLY"}]
。
当我看到返回的响应具有ROLE_APEX_SUPPORT
权威性时,我想做点什么。我如何解析响应以识别它?
let listOfAuthorities = this.userServices.getAuthorities();
listOfAuthorities
是上述响应的 JSON 数组。我如何解析并查看它是否具有支持角色作为响应?
要检查您的数据是否包含ROLE_APEX_SUPPORT
:
const json = [{"authority":"ROLE_APEX_SUPPORT"}, {"authority":"ROLE_APEX_READONLY"}];
const isROLE_APEX_SUPPORT = json.some(s=>s.authority === 'ROLE_APEX_SUPPORT');
console.log(isROLE_APEX_SUPPORT); // Output: true
要查找您的数据是否包含ROLE_APEX_SUPPORT
:
const json = [{"authority":"ROLE_APEX_SUPPORT"}, {"authority":"ROLE_APEX_READONLY"}];
const roleAlexSupport = json.find(s=>s.authority === 'ROLE_APEX_SUPPORT');
console.log(roleAlexSupport ); // OUTPUT: {authority: "ROLE_APEX_SUPPORT"}
此外,您可以使用 json.parse() 方法来解析 JSON:
var json = '{"result":true, "count":42}';
obj = JSON.parse(json);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句