techie20
techie20

Reputation: 508

How can I access the data inside this JSON?

var USER_DETAILS= {
    "details": [
        {
            "name": "john",
            "passwd": "xyz",
            "email": "[email protected]",
            "contact": "87685778",
            "lastLogin": "Sun Aug 24 2014 23:30:54 GMT+0530 (India Standard Time)"
        }, 
        {
            "name": "peter",
            "passwd": "xyz",
            "email": "[email protected]",
            "contact": "09820984",
            "lastLogin": "Sun Aug 24 2014 23:41:04 GMT+0530 (India Standard Time)"
        }, 
        {
            "name": "s",
            "passwd": "123",
            "email": "[email protected]",
            "contact": "3435",
            "lastLogin": "Mon Aug 25 2014 00:05:45 GMT+0530 (India Standard Time)"
        }, 
        {
            "name": "y",
            "passwd": "k",
            "email": "[email protected]",
            "contact": "87685778",
            "lastLogin": "Mon Aug 25 2014 00:12:59 GMT+0530 (India Standard Time)"
        }, 
        {
            "name": "johny",
            "passwd": "234",
            "email": "[email protected]",
            "contact": "34543",
            "lastLogin": "Mon Aug 25 2014 00:20:44 GMT+0530 (India Standard Time)"
        }
    ]
}

I have this jason data now I need to access the name "John". I have tried accessing it like USER_DETAILS.details[i].name but I am getting error USER_DETAILS.details is undefined.

Here is my rest of the code for accessing JSON

function check_details()
{
var users=JSON.parse(localStorage.getItem('USER_DETAILS'));
for (var key in users)
  {
  alert(users.details[0].name);
  }
}

Upvotes: 0

Views: 81

Answers (3)

James
James

Reputation: 774

Try this:

function check_details()
{
    var users=JSON.parse(localStorage.getItem('USER_DETAILS'));
    for (var user of users.details)
    {
        console.log(user.name);
    }
}

You're trying to loop over users which only has one object details, so you need to loop through the users.details object to get all the users

jsfiddle demo (without using localStorage)

For reference, documentation about for...of loops is on MSDN.

Upvotes: 0

alkathirikhalid
alkathirikhalid

Reputation: 897

error USER_DETAILS.details is undefined, because of a syntax error in your JSON String, missing ' and ''...

  • Yours: ={"details":[{"name":"john",...

  • Correct: ='{"details":[''{"name":"John",...

This is a working example

var USER_DETAILS = '{"details":[' +
'{"name":"John","passwd":"1234", "email":"[email protected]" },' +

'{"name":"Anna","passwd":"5678", "email":"[email protected]" },' +

'{"name":"Peter","passwd":"9945", "email":"[email protected]" }]}';

obj = JSON.parse(USER_DETAILS);
document.getElementById("paragraphid").innerHTML =
obj.details[0].name + " " + obj.details[0].email;

Best Practice:

  • Indent and concatenate your JSON String (+) per entity/row to distinguish the individual properties and values for easy readability

Upvotes: 0

Dalorzo
Dalorzo

Reputation: 20024

Try something like this:

USER_DETAILS.details[0].name

USER_DETAILS it is an object with a property details that is an Array and it 'John' is in the first instance of your array.

All together works fine on my end like this:

var USER_DETAILS= /* your sample code */;
console.log( USER_DETAILS.details[0].name);

Upvotes: 2

Related Questions