如何在Ionic / Angular中访问json对象

时间:2017-03-23 18:35:22

标签: javascript angularjs json ionic-framework

我需要访问此json中的firstBill值:

[{
    "companyName": "Madrid",
    "producCompanyId": 4,
    "productCompanyName": "Empresa1",
    "state": {
        "id": 5,
        "description": "Completada sin precios"
    },
    "message": "\nHay otra empresa igual en ACTIVO                                              "
}, {
    "companyName": "Barcelona",
    "producCompanyId": 10,
    "productCompanyName": "Empresa2",
    "companyResultId": "889",
    "state": {
        "id": 4,
        "description": "Completada con precios"
    },
    "prices": {
        "priceList": [{
            "priceId": 699614,
            "companyPriceId": "1091931204",
            "firstBill": 281.96,
            "issuable": true
        }, {
            "priceId": 699609,
            "companyPriceId": "1091931204",
            "firstBill": 251.64,
            "issuable": true
        }, {
            "priceId": 699611,
            "companyPriceId": "1091931204",
            "firstBill": 302.6,
            "issuable": true
        }]
    },
    "message": ""
}]

现在我有了这段代码但由于其他(前一个json中的第一个)对象中未定义的prices jsonarray而出现错误:

<ion-content class="home">
  <ion-list>
    <ion-item *ngFor="let oferta of ofertas">
      <h2>{{oferta.companyName}}</h2>
      <pre>{{ oferta.prices.priceList[0] ? oferta.prices.priceList[0] | json }}</pre>
    </ion-item>
  </ion-list>
</ion-content>

1 个答案:

答案 0 :(得分:0)

尝试以下修改后的代码。可能这可以帮到你。我添加了一些嵌套div结构的ngIf。

 <ion-list>
          <ion-item *ngFor="#oferta of ofertas">
            <h2>{{oferta.companyName}}</h2>
                  <div *ngIf="oferta.prices">
                   <div *ngFor="#plist of oferta.prices.priceList">
                          <pre>{{ plist | json }}</pre>
                  </div>
            </div>
          </ion-item>
       </ion-list>


 this.ofertas = [{
        "companyName": "Madrid",
        "producCompanyId": 4,
        "productCompanyName": "Empresa1",
        "state": {
            "id": 5,
            "description": "Completada sin precios"
          },

          "message": "\nHay otra empresa igual en ACTIVO                                              "
      }, {
          "companyName": "Barcelona",
          "producCompanyId": 10,
          "productCompanyName": "Empresa2",
          "companyResultId": "889",
          "state": {
              "id": 4,
              "description": "Completada con precios"
          },
          "prices": {
              "priceList": [{
                  "priceId": 699614,
                  "companyPriceId": "1091931204",
                  "firstBill": 281.96,
                  "issuable": true
              }, {
                  "priceId": 699609,
                  "companyPriceId": "1091931204",
                  "firstBill": 251.64,
                  "issuable": true
              }, {
                  "priceId": 699611,
                  "companyPriceId": "1091931204",
                  "firstBill": 302.6,
                  "issuable": true
              }]
          },
          "message": ""
      }]