【发布时间】:2018-05-12 10:52:25
【问题描述】:
我的 JSON 是什么样子的:
{
"reportSections": [
{
"name": "...",
"display": true,
"nav": false,
"reportGroups": {
"reports": [
{
"name": "...",
"url": "reportLibrary.tableau",
"nav": true,
"params": {
"reportName": "clientRelationshipReport",
"reportTitle": "Client Relationship Report",
"reportUrl": "...",
"reportHeight": "4030px"
},
"filters": "clientName,brokerName,entity,budgetClass,practice,office"
}
]
}
}
]
}
组件中的我的 HTML 文件模板
<li *ngFor = "let sectionName of reportNavJSONMain.reportSections">
<span> {{ sectionName.name }} </span>
<ul>
<li *ngFor="let report of sectionName.reportGroups.reports">
<a *ngIf="report.nav"> {{report.name}} </a>
</li>
</ul>
</li>
我的 component.ts 方法是什么样的:
//<irrelevant code>
....
getReports(): any {
new Promise((resolve,reject)=>{
resolve(
this.reportNavService.getJSONReportMain()
)
}).then((data)=>{
// console.dir(JSON.parse(JSON.stringify(data)));
this.reportNavJSONMain = JSON.stringify(data);
console.dir(this.reportNavJSONMain )
})
}
...
//<irrelevant code>
我的服务代码:
//....
public getJSONReportMain(): Promise<any> {
return this.http.get('...')
.toPromise()
.then((response: Response)=>{
//console.log(JSON.stringify(response.json()))
this.reportNavJSON = JSON.stringify(response.json());
return this.reportNavJSON;
}).catch((error: Response)=>{
return Observable.throw('Error');
});
}
// ....
在 component.ts 文件中,我已经将 this.reportNavJSONMain 初始化为一个对象(我也将它初始化为一个对象数组)但是当我使用 console.log() 或 console.dir() 它时,它总是显示它作为“对象”。我尝试了 JSON.parse()、JSON.stringify() 和这两种方法,但都没有奏效。
我的目标是从this.reportNavJSONMain 访问reportSections,但是当我访问this.reportNavJSONMain.reportSections 时,reportSections 不是 this.reportNavJSONMain 的一部分。但是当我console.dir(this.reportNavJSONMain) 或console.log(this.reportNavJSONMain) 时,它会显示:
{"reportSections":[
{"name":"...","display":true,"nav":true,"reportGroups":
{"reports":
[{"name":"Client Relationship Report","url": .....}
【问题讨论】:
-
能否改进一下格式
-
@Zze,是的,我可以,什么是正确的格式让它看起来更可读?
标签: javascript json angular typescript