无法显示数据JSON
问题描述:
我的数据格式为:无法显示数据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”和“用户”是不能够迭代数据需要在这个问题上提供帮助。
谢谢。
答
感谢所有为您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]});
}
}
答
你采用了android工作室?如果是这样,我可能会帮助我现在一直在问相似的问题2天。我们需要更多的信息,但是,你的第一个步骤可能是将要创建一个JSON数组像“JSONArray ARR =新JSONArray的信息存储(在这里把你josn条件的对象,随后的ToString()方法)
+0
我正在使用角度 –
答
提取数据从shopping_cart
有items
数组,所以你需要一个循环这里
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 */
答
如果JSON编码然后解码它PLS .. 然后
foreach(data.shopping_cart as item)
{
barcode=item[0]["barcode"];
description=item[0]["description"];
}
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);
});
的可能的复制[?如何获得所有属性Javascript对象的值(不知道密钥)](https://*.com/questions/7306669/ how-to-get-all-properties-values-of-javascript-object-without-know-the-key) –
你有错误信息吗?你正在尝试迭代的代码?我们得到了什么 – Amit
@Amit我收到错误“无法读取的未定义的属性‘项目’” 我试图让shopping_cart.items数据 –