简体   繁体   English

如何从Angular5和Ionic3中的json格式中检索数据

[英]How to retrieve data from json format in Angular5 and Ionic3

I am trying to fetch data from products api. 我正在尝试从产品api获取数据。 I am getting the response from the json file but I don't know how to use this data for printing all the product names on the screen. 我从json文件中获得响应,但是我不知道如何使用此数据在屏幕上打印所有产品名称。

search.ts: search.ts:

export class SearchPage {
  searchQuery: string = '';
  items: string[];
  data:object={};
  constructor(public navCtrl: NavController, private apiService: ApiService) {
     this.initializeItems();
  }

  initializeItems() {
    this.apiService.productsCall().subscribe(response => {
      //console.log(response);
      if(response['status'] == 200) {
        // console.log(response['response']);
        this.data=response['response'];
        console.log(this.data);
      } else if(response['status'] == 500) {
        console.log(response['error'].sqlMessage);

      }
    });
    this.items=[
      'Knee',
      'Knee Cap'
    ];

  }
}

search.html: search.html:

<ion-grid *ngFor="let item of items">
    <ion-row>
      <ion-col col-2>
        <img src='../assets/imgs/doctor.png'>
      </ion-col>
      <ion-col col-offset-1></ion-col>
      <ion-col col-9>
        Product name goes here
      </ion-col>
    </ion-row>
  </ion-grid>

json json

{"status":200,"error":null,"response":[{"product_id":1,"name":"knee cap","price":1290,"weight":0.4,"short_desc":"Neck Pain Relief Cervical Soft Pillo","long_desc":"Perfect for side/back sleepers","category_id":3,"units_in_stock":8,"units_on_order":7,"discount_available":10,"images":"images/Knee_Cap_1 .png"},{"product_id":2,"name":"soft pillow","price":1299,"weight":0.3,"short_desc":"Neck Pain Relief Cervical Soft Pillow ","long_desc":"Perfect for side/back sleepers","category_id":1,"units_in_stock":10,"units_on_order":8,"discount_available":12,"images":null},{"product_id":3,"name":"wrist brace","price":250,"weight":0.12,"short_desc":"Healthgenie Wrist Brace with Thumb Support One Size Fits Most","long_desc":"Helps relieve weak or sore wrist ,Comfortable pressure and body heat retention ,Provides the highest level of comfort","category_id":2,"units_in_stock":8,"units_on_order":7,"discount_available":10,"images":null}]} {“状态”:200,“错误”:空,“响应”:[{““产品ID”:1,“名称”:“护膝”,“价格”:1290,“重量”:0.4,“ short_desc”: “颈痛缓解颈椎柔软枕头”,“ long_desc”:“非常适合侧卧/后卧枕”,“ category_id”:3,“ units_in_stock”:8,“ units_on_order”:7,“ discount_available”:10,“ images”: “ images / Knee_Cap_1 .png”},{“ product_id”:2,“ name”:“软枕头”,“ price”:1299,“ weight”:0.3,“ short_desc”:“颈痛缓解颈椎软枕”, “ long_desc”:“完美的侧卧/后卧铺”,“ category_id”:1,“ units_in_stock”:10,“ units_on_order”:8,“ discount_available”:12,“ images”:null},{“ product_id”:3 ,“名称”:“腕托”,“价格”:250,“重量”:0.12,“ short_desc”:“拇指掌支撑最适合的Healthgenie腕带”,“ long_desc”:“帮助缓解手腕酸软或疼痛,舒适的压力和保暖性,提供最高的舒适度”,“ category_id”:2,“ units_in_stock”:8,“ units_on_order”:7,“ discount_available”:10,“ images”:null}]}

Please try this code, maybe this will help you. 请尝试使用此代码,也许会对您有所帮助。

<ion-grid *ngFor="let item of data">
<ion-row>
  <ion-col col-2>
    <img src='../assets/imgs/doctor.png'>
  </ion-col>
  <ion-col col-offset-1></ion-col>
  <ion-col col-9>
   {{item.name}}
  </ion-col>
</ion-row>

Instead of binding to this.data you can map your response immediately to this.items and using it in your view. 无需绑定到this.data您可以立即将响应映射到this.items并在视图中使用它。

export class SearchPage {
  searchQuery: string = '';
  // make items hold everything about an item, not just the name
  items: any[];

  constructor(public navCtrl: NavController, private apiService: ApiService) {
     this.initializeItems();
  }

  initializeItems() {
    this.apiService.productsCall().subscribe(response => {
      //you might need response = JSON.parse(response) if it's still string format
      if(response.status == 200) {
         this.items = response.response;
      }
    }, error => {
        console.log("error occurred");
    });
  }
}

And print the item names accordingly in the view: 并在视图中相应地打印项目名称:

  <ion-grid *ngFor="let item of items">
    <ion-row>
      <ion-col col-2>
        <img src='../assets/imgs/doctor.png'>
      </ion-col>
      <ion-col col-offset-1></ion-col>
      <ion-col col-9>
        {{ item.name }} price: {{ item.price }} 
      </ion-col>
    </ion-row>
  </ion-grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM