【问题标题】:How read value from uri key json如何从 uri key json 读取值
【发布时间】:2022-01-19 22:59:57
【问题描述】:

我有这个对象

{
  'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812',
  'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1',
  'http://schemas.microsoft.com/ws/2008/06/identity/claims/role': 'User'
}

如何读取键“http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier”的值?

解决方案:

obj['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier']

【问题讨论】:

  • 你试过obj['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier']吗?
  • @Andy 谢谢它的工作原理!

标签: javascript json typescript


【解决方案1】:

您可以将对象的值放入数组中并访问该数组:

const object1 = {
     'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812',
      'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1',
      'http://schemas.microsoft.com/ws/2008/06/identity/claims/role': 'User'
    };
    
    const arr = (Object.values(object1));
    console.log(arr[0]);

【讨论】:

    【解决方案2】:

    要访问对象中的键值对,您可以使用Object.entries(yourObjName)

    foo = {
      'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812',
      'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1',
      'http://schemas.microsoft.com/ws/2008/06/identity/claims/role': 'User'
    }
    
    for (const [key, value] of Object.entries(foo)) {
      console.log(`${key}: ${value}`);
    }
    

    阅读更多关于Object.entries()的信息

    【讨论】:

      【解决方案3】:

      谢谢@Andy

      解决方案:

      obj['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier']
      

      【讨论】:

        猜你喜欢
        • 2020-03-11
        • 2019-11-25
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 2016-10-12
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        相关资源
        最近更新 更多