2017-06-13 203 views
0

我的數據格式爲:無法顯示數據JSON

{ 
    "_id": "593994b7163e6b0011c738cb", 
    "location_id": "58ec522a99da86001123ec47", 
    "customer_id": "premiereservices", 
    "user": { 
     "phone_num": "8587366808", 
     "balance": 98.05 
    }, 
    "cart_total": 3, 
    "shopping_cart": { 
     "items": [ 
     { 
      "barcode": "611269101713", 
      "description": "Red Bull Sugar Free 8.4oz", 
      "price": 3, 
      "taxable": false, 
      "tax_collected": 0 
     } 
     ] 
    }, 
    "Date": "2017-06-08T12:17:27.039Z", 
    "__v": 0 
    } 

我從「shopping_cart」和「用戶」是不能夠迭代數據需要在這個問題上提供幫助。

謝謝。

+0

的可能的複製[?如何獲得所有屬性Javascript對象的值(不知道密鑰)](https://stackoverflow.com/questions/7306669/ how-to-get-all-properties-values-of-javascript-object-without-know-the-key) –

+1

你有錯誤信息嗎?你正在嘗試迭代的代碼?我們得到了什麼 – Amit

+0

@Amit我收到錯誤「無法讀取的未定義的屬性‘項目’」 我試圖讓shopping_cart.items數據 –

回答

1

感謝所有爲您help.This正在爲我的要求。

這裏是工作代碼:

import {Component} from 'angular2/core' 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <ul *ngFor="let object of data"> 
     <li> 
      <ul *ngFor="let value of of ObjectKey(object)"><li> {{value.phone_num}}</li> </ul> 
    </li> 
    <li> 
    <ul *ngFor="let value1 of of ObjectKey(object.shopping_cart.items)"><li> {{value1.barcode}}</li> </ul> 
    </li> 


    </ul> 
    `, 
    directives: [], 
    styles: [` 
    `] 
}) 
export class App { 

    ObjectKey(obj){ 
    return Object.keys(obj).map((key)=>{ return obj[key]}); 
    } 
} 
-1

你採用了android工作室?如果是這樣,我可能會幫助我現在一直在問相似的問題2天。我們需要更多的信息,但是,你的第一個步驟可能是將要創建一個JSON數組像「JSONArray ARR =新JSONArray的信息存儲(在這裏把你josn條件的對象,隨後的ToString()方法

+0

我正在使用角度 –

1

提取數據從shopping_cartitems數組,所以你需要一個循環這裏

For example:

let a = { 
    "shopping_cart": { 
     "items": [ 
     { 
      "barcode": "611269101713", 
      "description": "Red Bull Sugar Free 8.4oz", 
      "price": 3, 
      "taxable": false, 
      "tax_collected": 0 
     } 
     ] 
    } 
} 

if(a.shopping_cart.items.length) { 
    for(let i: number = 0; i < a.shopping_cart.items.length; i++) { 
    console.log(a.shopping_cart.items[i].barcode); 
    console.log(a.shopping_cart.items[i].description); /* etc you can here */ 
    } 
} 

user

0123中提取數據
let a = { 
    "user": { 
     "phone_num": "8587366808", 
     "balance": 98.05 
     }, 
    } 

console.log(a['user']['phone_num']) /* etc you can here */ 
0

如果JSON編碼然後解碼它PLS .. 然後

foreach(data.shopping_cart as item) 
{ 
barcode=item[0]["barcode"]; 
description=item[0]["description"]; 
} 

0是索引

0

創建data.model.ts

import { User } from './user.model'; 
import { ShoppingCart } from './shopping-cart.model'; 

export class Data { 
    _id: string; 
    location_id: string; 
    customer_id: string; 
    user: User; 
    cart_total: number; 
    shopping_cart: ShoppingCart; 
    Date: string; 
    __v: number; 
} 

創建user.model .TS

export class User { 
    phone_num: string; 
    balance: number; 
} 

創建購物,cart.model.ts

import { Item } from './item.model'; 

export class ShoppingCart { 
    items: Item[]; 
} 

創建item.model.ts

export class Item { 
    barcode: string; 
    description: string; 
    price: number; 
    taxable: boolean; 
    tax_collected: number; 
} 

然後

let str: string = 'your json string'; 
let data: Data = JSON.parse(str); 
console.log(data.user); 
data.shopping_cart.items.forEach(item => { 
    console.log(item); 
});