javascript - 以角度 6 将 Json 字符串转换为 json 数组
问题描述
嗨,我想将 Json 字符串中的 RestController 响应转换为 json 数组,这是我在控制台中打印的 json 数组字符串。
'{"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"]}'
我想将它转换为 json 数组,以便我可以迭代它,
我的要求是迭代数组并将键打印为标签,将值打印为 slect 选项
例子:
印象作为标签,“预算”、“点击率”、“广告系列”、“广告系列状态”、“点击次数”、“转化率”作为选择选项。
这是我的迭代代码
<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>
我的 .ts 课程
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;
}
);
解决方案
this.clientService.getHeaders().subscribe(
(res) => {
console.log(res);
let result= <any>res;
this.mapper= result;
console.log(this.mapper);
this.ismapped=false;
}
);
无需进行字符串化然后解析。只需将响应转换为any,然后您就可以将其用作数组。
推荐阅读
- python - 为什么我不能从 python 的类中导入函数?
- ios - iOS 动态表格视图周围有一个边框
- php - 在 osticket 中将自定义字段添加到数据库
- xml - XSLT:使用 XSLT 进行 XML 到 XML 的转换
- ios - 导航到新 UIViewController 的不同方式有什么区别?
- c - 有人可以告诉我错误,因为 pthread_join 不起作用
- c++ - CGAL - 将数据添加到 Triangle 类的正确方法
- laravel - 更好地理解 laravel 中的中间件保护
- java - 我如何从线程返回值
- javascript - 如何将选定的选项值设置为 null 或 undefined