2012-11-25 59 views
1

我有一個JSON對象(jsencodedata)印在Javascript中這樣的:訪問JSON對象

document.write(jsencodedata); 

它打印:

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

我的問題是如何讓只有第一個用戶或密碼還是隻有第二個? 我是新來的,請原諒我這個愚蠢的問題?

+0

'var arr = JSON.parse(jsencodedata);',然後'arr [0]'得到第一個對象,'arr [1]'得到第二個對象。 –

回答

3

嘗試此(假設jsencodeddata是JSON字符串):

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

這將JSON字符串轉換爲對象的實際的數組。

0

嘗試

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

陣列索引告訴你什麼對象訪問[0]表示要訪問的第一對象和[1]表示要訪問的第二個目的。

0

假設使用的是a browser with native JSON support,則:

var arrData = JSON.parse(jsencodedata); 

將解析JSON字符串到一個數組,然後:

arrData[0]是所述第一用戶。
arrData[0].user是第一個用戶的名字。
arrData[0].Pass是第一個用戶的密碼。

arrData[1]是第二個用戶。
arrData[1].user是第二個用戶的名字。
arrData[1].Pass是第二個用戶的密碼。