无法显示数据json

时间:2017-06-13 05:49:00

标签: json angular

我的数据格式为:

{
    "_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”中的数据,“user”需要有关此问题的帮助。

感谢。

5 个答案:

答案 0 :(得分:1)

shopping_cart中提取数据items数组,因此您需要在此处使用for循环

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

的数据
 let a = {
  "user": {
         "phone_num": "8587366808",
         "balance": 98.05
      },
  }

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

答案 1 :(得分:1)

感谢大家的帮助。这符合我的要求。

这是工作代码:

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]});
  }
}

答案 2 :(得分:0)

如果它是json编码然后解码它请.. 然后

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

0是索引

答案 3 :(得分: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;
}

创建shopping-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);
});

答案 4 :(得分:-1)

你正在使用android studio吗?如果是这样,我可以帮助我2天来一直在问类似的问题。我们需要更多信息,但是,你的第一步可能是创建一个json数组来存储信息,例如&#34; JSONArray arr = new JSONArray(在这里放入你的josn条件对象,后跟一个.toString( )方法