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
2 Answers
Reset to default 2this.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
版权声明:本文标题:javascript - Convert Json String to json Arrays in angular 6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744328461a2600843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论