admin管理员组

文章数量:1401939

Hi I want to convert the RestController Response which is in Json String to json array here is my json array sting which i have printed in console.

'{"Impressions":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"],"Clicks":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"],"Revenue":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"]}'

I want to convert it to json array so that i can iterate it,

My requirement is to iterate the array and print key as label and value as slect options

Example:

Impression as label and "Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate" as select options.

Here is my code for iteration

<div>
   <form *ngFor ="let map of mapper">
      <mat-form-field>
         <mat-select placeholder="{{map}}">
            <!--<mat-option>None</mat-option>-->
             <mat-option *ngFor="let option of map"  [value]="option"> 
                {{option}}
             </mat-option>
         </mat-select>
      </mat-form-field>
    </form>
</div>

my .ts class

this.clientService.getHeaders().subscribe(
      (res) => {
          console.log(res);
          let resSTR = JSON.stringify(res);
          let resJSON = JSON.parse(resSTR);
          this.mapper=Array.of(resJSON._body);
          console.log(this.mapper);
          this.ismapped=false;
      }
);

Hi I want to convert the RestController Response which is in Json String to json array here is my json array sting which i have printed in console.

'{"Impressions":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"],"Clicks":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"],"Revenue":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"]}'

I want to convert it to json array so that i can iterate it,

My requirement is to iterate the array and print key as label and value as slect options

Example:

Impression as label and "Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate" as select options.

Here is my code for iteration

<div>
   <form *ngFor ="let map of mapper">
      <mat-form-field>
         <mat-select placeholder="{{map}}">
            <!--<mat-option>None</mat-option>-->
             <mat-option *ngFor="let option of map"  [value]="option"> 
                {{option}}
             </mat-option>
         </mat-select>
      </mat-form-field>
    </form>
</div>

my .ts class

this.clientService.getHeaders().subscribe(
      (res) => {
          console.log(res);
          let resSTR = JSON.stringify(res);
          let resJSON = JSON.parse(resSTR);
          this.mapper=Array.of(resJSON._body);
          console.log(this.mapper);
          this.ismapped=false;
      }
);
Share Improve this question edited Aug 9, 2018 at 12:40 Augustin R 7,8393 gold badges29 silver badges54 bronze badges asked Aug 9, 2018 at 11:18 nidhinidhi 3293 gold badges6 silver badges22 bronze badges 3
  • What is the question? – Venomy Commented Aug 9, 2018 at 11:33
  • @Venomy I want to convert Json String in to Json array of type User defined Object class and use that array to iterate in *ngfor – nidhi Commented Aug 9, 2018 at 11:58
  • You mean JSON string to JavaScript array. There's no such thing as a JSON array/object. – phuzi Commented Aug 9, 2018 at 12:42
Add a ment  | 

2 Answers 2

Reset to default 2
this.clientService.getHeaders().subscribe(
  (res) => {
      console.log(res);
      let result= <any>res;
      this.mapper= result;
      console.log(this.mapper);
      this.ismapped=false;
  }
);

No need to go into stringifying and then parsing. Just cast the response to any and then you can use it as an array.

let str = '{"Impressions":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"],"Clicks":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"],"Revenue":["Budget","CTR","Campaign","Campaign state","Clicks","Conv. rate"]}'

Object.keys(JSON.parse(str)).map(arr => {
  return `${arr}: ${JSON.parse(str)[arr].join(', ')}`;
});

Do whatever logic you want in return statement. Return it as a string, or as an array, or as an object.

本文标签: javascriptConvert Json String to json Arrays in angular 6Stack Overflow