如何从离子的嵌套json响应中打印数据

时间:2021-02-03 21:44:30

I have been stuck from like a week as to how do I print the data from a nested json response which I have been trying almost day and night. I have googled for all possible help but i couldn't find any help. I have no other option and hence I am seeking help!

关于如何从嵌套的json响应中打印数据,我几乎是一天一夜地被困在一周。我已经google了所有可能的帮助,但我找不到任何帮助。我别无选择,因此我正在寻求帮助!

my response

{
    "data": {
        "1": {
            "Sales": {
                "Total Sales": "-",
                "Non-VAT": "-",
                "Baskets": "-",
                "Avg Basket": "-",
                "Known Customers": "-",
                "Discounts": "-",
                "Owners": "-",
                "Staff": "-",
                "Margin": "-",
                "Profit": "-"
            },
            "Accounts": {
                "Register": "-",
                "Cash Close Date": "-",
                "Cash Excess\/ Short": "-",
                "Cash In Hand": "-"
            },
            "Alerts": {
                "-ve Batches": "-",
                "-ve QOH": "-",
                "Items": "-",
                "Stock": "-",
                "Replication": "-"
            }
        },
        "2": {
            "Sales": {
                "Total Sales": "-",
                "Non-VAT": "-",
                "Baskets": "-",
                "Avg Basket": "-",
                "Known Customers": "-",
                "Discounts": "-",
                "Owners": "-",
                "Staff": "-",
                "Margin": "-",
                "Profit": "-"
            },
            "Accounts": {
                "Register": "-",
                "Cash Close Date": "-",
                "Cash Excess\/ Short": "-",
                "Cash In Hand": "-"
            },
            "Alerts": {
                "-ve Batches": "-",
                "-ve QOH": "-",
                "Items": "-",
                "Stock": "-",
                "Replication": "-"
            }
        },
        "3": {
            "Sales": {
                "Total Sales": "-",
                "Non-VAT": "-",
                "Baskets": "-",
                "Avg Basket": "-",
                "Known Customers": "-",
                "Discounts": "-",
                "Owners": "-",
                "Staff": "-",
                "Margin": "-",
                "Profit": "-"
            },
            "Accounts": {
                "Register": "-",
                "Cash Close Date": "-",
                "Cash Excess\/ Short": "-",
                "Cash In Hand": "-"
            },
            "Alerts": {
                "-ve Batches": "-",
                "-ve QOH": "-",
                "Items": "-",
                "Stock": "-",
                "Replication": "-"
            }
        },
        "4": {
            "Sales": {
                "Total Sales": "-",
                "Non-VAT": "-",
                "Baskets": "-",
                "Avg Basket": "-",
                "Known Customers": "-",
                "Discounts": "-",
                "Owners": "-",
                "Staff": "-",
                "Margin": "-",
                "Profit": "-"
            },
            "Accounts": {
                "Register": "-",
                "Cash Close Date": "-",
                "Cash Excess\/ Short": "-",
                "Cash In Hand": "-"
            },
            "Alerts": {
                "-ve Batches": "-",
                "-ve QOH": "-",
                "Items": "-",
                "Stock": "-",
                "Replication": "-"
            }
        },
        "5": {
            "Sales": {
                "Total Sales": "-",
                "Non-VAT": "-",
                "Baskets": "-",
                "Avg Basket": "-",
                "Known Customers": "-",
                "Discounts": "-",
                "Owners": "-",
                "Staff": "-",
                "Margin": "-",
                "Profit": "-"
            },
            "Accounts": {
                "Register": "-",
                "Cash Close Date": "-",
                "Cash Excess\/ Short": "-",
                "Cash In Hand": "-"
            },
            "Alerts": {
                "-ve Batches": "-",
                "-ve QOH": "-",
                "Items": "-",
                "Stock": "-",
                "Replication": "-"
            }
        },
        "6": {
            "Sales": {
                "Total Sales": "-",
                "Non-VAT": "-",
                "Baskets": "-",
                "Avg Basket": "-",
                "Known Customers": "-",
                "Discounts": "-",
                "Owners": "-",
                "Staff": "-",
                "Margin": "-",
                "Profit": "-"
            },
            "Accounts": {
                "Register": "-",
                "Cash Close Date": "-",
                "Cash Excess\/ Short": "-",
                "Cash In Hand": "-"
            },
            "Alerts": {
                "-ve Batches": "-",
                "-ve QOH": "-",
                "Items": "-",
                "Stock": "-",
                "Replication": "-"
            }
        },
        "7": {
            "Sales": {
                "Total Sales": "-",
                "Non-VAT": "-",
                "Baskets": "-",
                "Avg Basket": "-",
                "Known Customers": "-",
                "Discounts": "-",
                "Owners": "-",
                "Staff": "-",
                "Margin": "-",
                "Profit": "-"
            },
            "Accounts": {
                "Register": "-",
                "Cash Close Date": "-",
                "Cash Excess\/ Short": "-",
                "Cash In Hand": "-"
            },
            "Alerts": {
                "-ve Batches": "-",
                "-ve QOH": "-",
                "Items": "-",
                "Stock": "-",
                "Replication": "-"
            }
        }
    }
}

my ts file

我的ts档案

export class HomePage {
 data:any;

 keyss:any;

 alldata: Observable<any>; //<-- this add first
 keys:Observable<any>;
  constructor(public navCtrl: NavController,
    private http: Http) {
    // window.open("http://google.com",'_system', 'location=yes');
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({ headers: headers });

    this.http.get('http://url/file/file.php',options) 
    .map(res => res.json())

    .subscribe(data => {
      console.log("Api Fired",data);
      this.data =data;
      var info=JSON.parse(JSON.stringify(this.data));
     this.alldata = info.data; //<--this add
     console.log("Api Fired 1",this.alldata);
      this.keys = this.alldata;

       });  
}


}

my html file

我的html文件

  <ion-content padding>
      <ion-content>
        <ion-grid>
          <ion-row>
         <ion-col *ngFor = "let key of keys?.alldata">

         </ion-col>
         <ion-col>

         </ion-col>
         <ion-col>

         </ion-col>
          </ion-row>
        </ion-grid>

  </ion-content>

Please help me guys cz this is my project work and i need help. Please

请帮助我们cz这是我的项目工作,我需要帮助。请

1 个解决方案

#1


0  

What you can do is make a for in like this:

你能做的就是这样做:

for(let i in this.alldata) console.log(this.alldata[i])

If not work try of instead of in I don’t remember which one is

如果不工作尝试而不是我不记得是哪一个

#1


0  

What you can do is make a for in like this:

你能做的就是这样做:

for(let i in this.alldata) console.log(this.alldata[i])

If not work try of instead of in I don’t remember which one is

如果不工作尝试而不是我不记得是哪一个