使用*ngFor检索和显示json对象
我想从angular2中的json文件中检索信息 这是我在组件中使用的模板:使用*ngFor检索和显示json对象,json,angular,pipe,ngfor,Json,Angular,Pipe,Ngfor,我想从angular2中的json文件中检索信息 这是我在组件中使用的模板: <button (click)="onTestGet()">GET REQUEST</button><br> <p>{{getData}}</p> <button (click)="onTestPost()">POST REQUEST</button><br>
<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>
我想检索例如“status”和“type”,但检索的字符数组如下:
价值:{
值:“
价值: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());
}
this.http.get('/myfile.json').map(res => res.json());
有人知道为什么我检索的是字符数组而不是我想要的字符串数组吗?
我想具体说明我遵循以下示例:
我认为这不是解析json的问题,而是检索它以显示
感谢您的帮助我不知道如何获取数据,但您似乎忘记了解析它们(即,将其转换为JSON对象),因为您迭代的是字符串而不是对象 在循环中,第一个元素是
{
内容的第一个字符,第二个元素是“
,依此类推
要加载JSON文件,您应该使用以下内容:
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());
}
this.http.get('/myfile.json').map(res => res.json());
我不知道如何获取数据,但似乎您忘记了解析它们(即,将其转换为JSON对象),因为您迭代的是字符串而不是对象 在循环中,第一个元素是
{
内容的第一个字符,第二个元素是“
,依此类推
要加载JSON文件,您应该使用以下内容:
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());
}
this.http.get('/myfile.json').map(res => res.json());
谢谢Thierry,但是关于你之前的问题,我看了一下,你从json中正确地检索到数据了吗?这与我想记录的事情是一样的。你能提供你的回复的内容吗?特别是
内容类型
标题。谢谢!看看这里和底部我回复的内容类型:firstheaders.append('Content-Type','application/json');firstheaders.append('Authorization','Basic YWRtaW46YWRtaW4='));是您为请求放置的标题?可以在网络选项卡的开发工具中看到响应标题…是的,它们是我为请求放置的标题。内容类型响应是相同的:application/JSonTanks Thierry,但是关于我之前看到的问题,您是否正确地从json中检索了数据?这与我想说的相同ke to Doc你能提供你的回复的内容吗?特别是内容类型
标题。谢谢!看看这里和底部我回复的内容类型:firstheaders.append('content-Type','application/json');firstheaders.append('Authorization','Basic YWRtaW46YWRtaW4='));是您为请求放置的标题?响应的标题可以在“网络”选项卡的“开发工具”中看到……是的,它们是我为请求放置的标题。响应的内容类型相同:application/json