1

I have a json object(jsencodedata) that is printed in Javascript like that:

document.write(jsencodedata); 

It prints:

[{"user":"John","Pass":"abc123"},{"user":"John2","Pass":"abcdef"}]

My question is how to get only the first user or password or only the second one? I'm new to this, so please excuse me for the stupid question?

1
  • var arr = JSON.parse( jsencodedata );, and then arr[0] to get the first object, and arr[1] to get the second object. Commented Nov 25, 2012 at 21:48

3 Answers 3

3

Try this (assuming jsencodeddata is a JSON string):

var users = JSON.parse(jsencodeddata);
console.log(users[0]); // the first user object
console.log(users[1]['Pass']); // second user's password

That will convert the JSON string into an actual array of objects.

Sign up to request clarification or add additional context in comments.

Comments

0

Try

jsencodedata[0].user;  //returns "John"
jsencodedata[0].Pass;  //returns "abc123"

The array indices tell you what object to access [0] means you are accessing the first object and [1] means you are accessing the second object.

Comments

0

Assuming you are using a browser with native JSON support, then:

var arrData = JSON.parse(jsencodedata);

will parse the JSON string into an array, and then:

arrData[0] is the first user.
arrData[0].user is the first user's name.
arrData[0].Pass is the first user's password.

arrData[1] is the second user.
arrData[1].user is the second user's name.
arrData[1].Pass is the second user's password.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.