我有JSON
以下的API
。
{
"categories":[
{
"id":"20",
"parent_id":"3",
"name":"Khau Galli",
"description":null,
"sort_order":"58",
"is_visible":"1",
"created_at":"2018-05-20 07:47:36",
"updated_at":"2018-06-12 13:50:38",
"icon":null
},
{
"id":"22",
"parent_id":"3",
"name":"Apparel Wear",
"description":null,
"sort_order":"59",
"is_visible":"1",
"created_at":"2018-05-20 07:50:34",
"updated_at":"2018-06-12 13:50:38",
"icon":null
},
{
"id":"204",
"parent_id":"3",
"name":"Footwear",
"description":null,
"sort_order":"227",
"is_visible":"1",
"created_at":"2018-05-24 10:26:27",
"updated_at":"2018-06-12 13:50:38",
"icon":null
},
{
"id":"205",
"parent_id":"3",
"name":"Fruits & Vegetable Vendor",
"description":null,
"sort_order":"228",
"is_visible":"1",
"created_at":"2018-05-24 10:28:26",
"updated_at":"2018-06-12 13:50:38",
"icon":null
},
{
"id":"327",
"parent_id":"3",
"name":"Paan Shop",
"description":null,
"sort_order":"340",
"is_visible":"1",
"created_at":"2018-06-04 10:01:02",
"updated_at":"2018-06-12 13:50:38",
"icon":null
}
]
}
我想从离子搜索栏的响应中取名字。但是,当我console.log name
时,它的含义是不确定的。我写了下面的代码
this.authService.postData("id=" + id, "category").then((result) => {
this.resposeData = result;
this.categories = this.resposeData.categories;
this.categoriesName = this.categories.name;
console.log(this.categoriesNamem);
//this.listings = this.resposeData.data.list.data;
//console.log(this.categories);
// localStorage.setItem('userData', JSON.stringify(this.resposeData) )
// this.navCtrl.push(Login);
this.loading.dismiss();
})
即使我在JSON.stringify
上尝试了this.categories
。但仍给未定义。请帮忙。我想在下面的结构中获得所有名称以进行离子搜索。
initializeItems() {
this.items = [
'Amsterdam',
'Bogota',
...
];
}
this.item
来自name
的地方
答案 0 :(得分:1)
Categories是一个数组。尝试this.categories [0] .name。这将为您提供第一类的名称
答案 1 :(得分:1)
您只需要遍历类别并将每个名称字段存储到数组中即可。
this.authService.postData("id=" + id, "category").then((result) => {
this.resposeData = result;
this.categories = this.resposeData.categories;
// Iterate through categories and store each name into this.names
this.names = this.categories.map(cat => cat.name);
//this.listings = this.resposeData.data.list.data;
//console.log(this.categories);
// localStorage.setItem('userData', JSON.stringify(this.resposeData) )
// this.navCtrl.push(Login);
this.loading.dismiss();
});
答案 2 :(得分:0)
类别是对象的数组,所以我认为您必须尝试类似的事情
this.categoriesName = this.categories[0].name;