0

My data format is :

{
    "_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
  }

I am not able iterate data from "shopping_cart" and "user" need help on this issue.

Thanks.

  • Possible duplicate of [How to get all properties values of a Javascript Object (without knowing the keys)?](https://stackoverflow.com/questions/7306669/how-to-get-all-properties-values-of-a-javascript-object-without-knowing-the-key) – alex kucksdorf Jun 13 '17 at 05:50
  • 1
    Do you have an error message? The code you're trying to iterate with? We got nothing – Amit Jun 13 '17 at 05:51
  • @Amit I am getting error "Cannot read property 'items' of undefined" I am trying to get shopping_cart.items data – Salma Kona Gugly Jun 13 '17 at 05:59
  • This error message suggests you are trying to access a property that does not exist. Please try to make sure your object has been initialized properly. – alex kucksdorf Jun 13 '17 at 06:02

5 Answers5

1

to extract data from shopping_cart has items array so you will need a for loop here

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 */
  }
}

to extract data for user

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

 console.log(a['user']['phone_num']) /* etc you can here */
mayur
  • 3,558
  • 23
  • 37
1

Thanks all for your help.This is working for my requirement.

Here is working code:

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

If it is json encoded then decode it pls.. Then

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

0 is the index

User123123
  • 19
  • 1
  • 7
0

Create 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;
}

Create user.model.ts

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

Create shopping-cart.model.ts

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

export class ShoppingCart {
    items: Item[];
}

Create item.model.ts

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

Then

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);
});
Milan Hlinák
  • 4,260
  • 1
  • 30
  • 41
-1

are you using android studio? If so I might be able to help I have been asking similary questions for 2 days now. we need more info but, your first step is probably going to be creating a json array to store the info like "JSONArray arr= new JSONArray(in here put your josn condition object, followed by a .toString() method)

Karanvir1
  • 59
  • 10