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

问题描述 投票:0回答:1

我想从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

angular multidimensional-array
1个回答
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(资源将有多个数据),那么让我知道我会更新答案。

© www.soinside.com 2019 - 2024. All rights reserved.