从api ionic angular 4中提取数据并解析

时间:2018-04-26 09:48:53

标签: angular ionic-framework

我有这个api,有从api获取数据的功能。 页面有pageelements,内页元素是数据,在数据字段中保存标题,文本,图像。 如何从现场数据中获取数据?

getMenu() {
return this.http.get('http://site.dev/api/menu/7');}

{
"id": 26,
"name": "Default",
"title": "default",
"pageelements": [
  {
    "id": 15,
    "page_id": 26,
    "element_type_id": 3,
    "data": "{\"image\":\"download.jpg\"}",
    "created_at": "2018-04-20 10:08:24",
    "updated_at": "2018-04-20 10:12:05",
    "position": 1
  },
  {
    "id": 16,
    "page_id": 26,
    "element_type_id": 1,
    "data": "{\"title\":\"Naslov neki\"}",
    "created_at": "2018-04-20 10:10:11",
    "updated_at": "2018-04-20 10:12:05",
    "position": 2
  },
  {
    "id": 18,
    "page_id": 26,
    "element_type_id": 1,
    "data": "{\"title\":\"Naslov 2\"}",
    "created_at": "2018-04-20 10:11:06",
    "updated_at": "2018-04-20 10:11:27",
    "position": 0
  },
  {
    "id": 19,
    "page_id": 26,
    "element_type_id": 3,
    "data": "{\"image\":\"download (1).jpg\"}",
    "created_at": "2018-04-20 10:11:36",
    "updated_at": "2018-04-20 10:11:52",
    "position": 4
  }
],

},

1 个答案:

答案 0 :(得分:0)

假设您的api数据位于页面对象中。然后使用此代码访问数据。

let pageelements = this.page.pageelements;
        for(let i=0; i<pageelements.length;i++) {
            console.log(pageelements[i].data);

            console.log(JSON.parse(pageelements[i].data).image);
            console.log(JSON.parse(pageelements[i].data).title);
        }

https://stackblitz.com/edit/angular-yn8tb5