如何从json中获取键和值对,如下所示:-

时间:2019-02-08 09:50:06

标签: arrays json typescript

我正在尝试从下面的json获取键和值,但不知道如何动态获取。用于Angular 5项目。

组件:-这是我要在组件中使用的方法。

 public AnalyticsRes(_AnalyticsResponse) {

      let ButtonAttribute = _AnalyticsResponse.ButtonAttribute.split(',');
      let Button2Attribute = _AnalyticsResponse.Button2Attribute.split(',');


      let key1 = ButtonAttribute['know-more1'];
      let key2 = ButtonAttribute['know-more2'];

      }


    I am not sure if syntax is correct kind of newbie in angular 

    and here is the HTML CODE:-


<div class="grid-i" *ngIf="response.Button1.trim() || response.Button2.trim()">
                    <a class="xyz" *ngIf="response.Button1.trim()" (click)="ManagaeNavigation(response.ButtonLink);defaultClick();"
                    href="{{response.ButtonLink}}" [analytics-promo]='response.BtnAnalytic ? response.BtnAnalytic : null'> 
                        <span class="buttonClass">{{response.ButtonText}}</span> 
                        <span class="arrowIcon"></span> 
                    </a>
                    <a class="xyz" *ngIf="response.Button2.trim()" href="{{response.Button2Link}}" 
                    [attr.data-analytics-promo]='response.Btn2Analytic ? response.Btn2Analytic : null'> 
                        <span class="buttonClass">{{response.Button2}}</span> 
                        <span class="arrowIcon"></span> 
                    </a>
                </div>

这是我的json:-

    {
        "Items": [
            {
                "Title": "Get it:",
                "field_background_color": "is-bg-color-blue",
                "ButtonLink": "/myUrl",
                "ButtonText": "Visit Site",
                "ButtonAnalytic": "link-analytics:first, link-analytics:second",
                "ButtonTwoLink": "http://dummy.com",
                "ButtonTwoText": "Know More",
                "Button2Analytic": "know-more:first, know-more:second",
                **"ButtonAttribute": "link-analytics:first, link-analytics:second",
                "Button2Attribute": "know-more:first, know-more:second",**
                "ButtonLinkAbsolute": "/abc",
                "ButtonTwoLinkAbsolute": "http://dummy.com",
                "Students": [
                    {
                        "title": "No",
                        "description": "Visit a <a href=\"https://www.google.com"> Outlet</a> dummy ",

                    },
                    {
                        "title": "No",
                        "description": "FREE <a href=\"https://www.google.com"> free </a> dummy.",

                    },
                    {
                        "title": "No",
                        "description": "Bring home",

                    }
                ]
            }
        ],
        "Status": "OK",
        "StatusMessage": "SUCCESS"
    }

请帮助我从:-

获取关键和价值

“ ButtonAttribute”:“链接分析:第一,链接分析:第二”,                     “ Button2Attribute”:“知道更多:第一,知道更多:第二”,

因为json中有逗号分隔的值,所以我必须在我的component方法中将它们放入数组中,并想在我的html中使用它们?这里的另一个挑战是我如何在角度6中将多个属性绑定到单个元素?代码代码将更有帮助。预先谢谢你。

0 个答案:

没有答案