我想从 angular2 中的 json 文件中检索信息。为此,我按照示例How to display json object using *ngFor创建了这个自定义管道
import {Pipe, PipeTransform} from 'angular2/core';
@Pipe({name: 'parseData'})
export class ParseDataPipe implements PipeTransform{
constructor(){}
transform(value, args:string[]) : any {
let keys = [];
for (let key in value) {
keys.push({
key: key,
value: value[key]
});
}
return keys;
}
}
这是我在组件中使用的模板:
<button (click)="onTestGet()">GET REQUEST</button><br>
<p>{{getData}}</p>
<button (click)="onTestPost()">POST REQUEST</button><br>
<p>{{postData}}</p>
<span *ngFor="#entry of postData | parseData">
<ul>
<!--<li>Key: {{entry.key}}, value: {{entry.value.status}}</li>;-->
<li>Value: {{entry.value}}</li>
</ul>
</span>
</div>
这是我的 json 文件:
{"status":"success","type":"cellSetData","data": {"epoch":6874,"cube":"EquityDer","axes":[{"id":-1,"hierarchies": [{"dimension":"Measures","hierarchy":"Measures"}],"positions": [[{"namePath":["pnl.SUM"],"captionPath":["pnl.SUM"],"properties": {"DISPLAY_INFO":0}}]]}],"cells":[{"ordinal":0,"value":-42973.21272120108,"formattedValue":"-42,973.21","properties":{}}],"defaultMembers":[{"dimension":"Measures","hierarchy":"Measures","path":["contributors.COUNT"],"captionPath":["Count"]},{"dimension":"Time","hierarchy":"HistoricalDates","path":["Mon Apr 11 02:00:00 CEST 2016"],"captionPath":["2016-04-11"]},{"dimension":"Epoch","hierarchy":"Epoch","path":["master"],"captionPath":["master"]}]}}
我想检索例如“状态”和“类型”,但我检索一个字符数组,如下所示:
值:{ 值:" 值:s 值:t 值:a 值:t 值:u 值:s 值:" 值::值:" 值:s 值:u 值:c 值:c 值:e 值: s 值: s 值:“ 值: ,值:” ...
我像这样解析我检索的对象:
postJSON(){
var param=JSON.stringify(
{"mdx":"SELECT FROM [EquityDerivativesCube] WHERE ([Measures].[pnl.SUM])"}
);
var firstheaders = new Headers();
firstheaders.append('Content-Type', 'application/json');
firstheaders.append('Authorization', 'Basic YWRtaW46YWRtaW4=');
return this._http
.post('http://localhost:9090/webservices/pivot/rest/v1/cube/query/mdx', param,{headers: firstheaders})
.map(res => res.json());
}
有谁知道为什么我检索字符数组而不是我想要的字符串数组?我想指定我遵循这些示例:使用 *ngFor 访问对象的键和值 Deep nested json to array in array / json rendering of Angular 2 *ngFor
我认为这不是解析json的问题,而是将其检索显示...
谢谢你帮助我