如何从角度中的嵌套json数组中获取值?

时间:2017-12-22 11:18:48

标签: angular multidimensional-array

我想从angular2中的嵌套json数组中获取值。 我想从以下json数组中获取 client_id

  

{“resources”:[{

"scope" : [ "clients.read", "clients.write" ],
"client_id" : "tc53EZ",
"resource_ids" : [ "none" ],
"authorized_grant_types" : [ "client_credentials" ],
"redirect_uri" : [ "http://ant.path.wildcard/**/passback/*", "http://test1.com" ],
"autoapprove" : [ "true" ],
"authorities" : [ "clients.read", "clients.write" ],
"token_salt" : "C7NBIz",
"allowedproviders" : [ "uaa", "ldap", "my-saml-provider" ],
"name" : "My Client Name",
"lastModified" : 1512452520895 } ],
     

“startIndex”:1,“itemsPerPage”:1,“totalResults”:1,
  “schemas”:[“http://cloudfoundry.org/schema/scim/oauth-clients-1.0”   ]}

任何人都可以帮助我。

谢谢&此致

Shilpa Kulkarni

1 个答案:

答案 0 :(得分:0)

如果您的数据是单一资源

    public getClientId(data) {
      let allClients: string[] = [];
      if(data && data.resources && data.resources.length > 0) {
        data.resources.forEach((d, i) => {
          allClients.push(d.client_id);
          if(data.resources.length === i + 1) {
            this.clients = allClients; // clients will be array of type number and will be declared in component
          }
        });
      } else
        return null;
    }

如果你想从资源中获取所有client_id(资源将有多个数据),那么让我知道我会更新答案。