如何在JSON响应中打印特定数据属性
我得到这样的安息。
ionViewDidLoad() {
console.log('ionViewDidLoad SonglistPage');
this.eid = this.backendprovider.getEventID();
console.log('eventID');
console.log(this.eid);
this.backendprovider.sendEventID(this.eid).subscribe((songs) => {
this.songslist = songs;
});
}

我想打印歌曲对象的标题。请帮我。谢谢
这就是我尝试过的。但它会打印所有对象数据
const myObjStr = JSON.stringify(songs);
console.log(myObjStr);

打印输出:
[{"id":{"timestamp":1518586352,"machineIdentifier":3380871,"processIdentifier":5056,"counter":16748217,"time":1518586352000,"date":1518586352000,"timeSecond":1518586352},"songId":1,"title":"Somebody's Me","artist":"Enrique Iglesias","genre":"Jazz"}]

答案 0 :(得分:0)
使用map
finction:
const songTitles = songs.map(a=>a.title);
console.log(JSON.stringify(songTitles));
答案 1 :(得分:0)
songs
变量是一个数组。您需要迭代数组以获得歌曲标题。
ionViewDidLoad() {
console.log('ionViewDidLoad SonglistPage');
this.eid = this.backendprovider.getEventID();
console.log('eventID');
console.log(this.eid);
this.backendprovider.sendEventID(this.eid).subscribe((songs) => {
this.songslist = songs;
songs.forEach((song) => {
console.log(song.title);
});
});
}
答案 2 :(得分:0)
如果您使用ajax和jQuery来获取响应,那么您可以执行以下操作:
$.ajax(function() {
url:'your endpoint url',
data: function(d){
//your data
}
success: function(response){
var songTittle = response['title'];
console.log(songTittle);//should print Somebody's Me
}
error: function(response) {
//error callback
}
});
但总的来说,像这样的response['tittle']
应该做的工作。
答案 3 :(得分:0)
它的数组结果可以使用* ngFor
打印出来<div *ngFor = "let song of songslist">
{{song.title}}
</div>