Pawan
Pawan

Reputation: 32331

Unable to parse JSON

I have a jSON as shown below

var data = [{
    "RestrntArea": "dfew"
}, {
    "RestrntArea": "Home"
}, {
    "RestrntArea": "Kiran"
}, {
    "RestrntArea": "Meridian"
}, {
    "RestrntArea": "Office"
}, {
    "RestrntArea": "SomeLocation"
}, {
    "RestrntArea": "Testing"
}, {
    "RestrntArea": "TestLOcation"
}, {
    "customer_id": "3"
}, {
    "mobile_number": "9876543211"
}];

How can i fetch customer_id from the above JSON

I have tried this way

alert(data.customer_id);

But its giving me undefined .

could anyone please help me

Upvotes: 0

Views: 52

Answers (1)

Roko C. Buljan
Roko C. Buljan

Reputation: 206565

Try with

data[8].customer_id // "3"

why 8? Cause you're using an Array which has the customer_id Object literal set in the 8th key index.

If you want to loop all your keys searching for an customer_id than:

for(var i=0; i<data.length; i++){
  if(data[i].hasOwnProperty('customer_id')){
    console.log(data[i].customer_id);           // "3"
  }
}

Upvotes: 2

Related Questions