2015-04-06 68 views
0

我有這樣的JSON對象:如何使用jQuery通過鍵搜索和獲取json行數據?

{ 

"1234A":{ 
    "apcdiv_staffname":"MOHD NASA", 
    "apcdiv_staffimage":"http://localhost/jknsapc/wp-content/uploads/2014/06/product-landing-layout-thumb.png", 
    "apcdiv_workplace":"HOSPITAL", 
    "apcdiv_grade":"U44", 
    "apcdiv_position":"PEGAWAI FARMASI", 
    "apcdiv_ic":"1234567" 
}, 
"1234B":{ 
    "apcdiv_staffname":"MOHTAR", 
    "apcdiv_staffimage":"http://localhost/jknsapc/wp-content/uploads/2014/06/product-landing-layout-thumb.png", 
    "apcdiv_workplace":"HOSPITAL", 
    "apcdiv_grade":"U44", 
    "apcdiv_position":"PERGIGIAN", 
    "apcdiv_ic":"7654321" 
}, 

我要搜索,並通過使用密鑰讀取JSON行數據。例如,如果我使用密鑰1234B進行搜索,它將返回屬於1234B密鑰的數據。從那裏我可以繼續抓住數據裏面,如apcdiv_staffname

"1234B":{ 

"apcdiv_staffname":"MOHTAR", 
"apcdiv_staffimage":"http://localhost/jknsapc/wp-content/uploads/2014/06/product-landing-layout-thumb.png", 
"apcdiv_workplace":"HOSPITAL", 
"apcdiv_grade":"U44", 
"apcdiv_position":"PERGIGIAN", 
"apcdiv_ic":"7654321" 

} 

如何使用jQuery實現它?我試圖谷歌,但無法找到相關的答案,也許是因爲我選擇的關鍵字。如果使用PHP陣列,我可以簡單地使用此代碼:

$object_row = $object['1234B']; 
$apcdiv_staffname = $object_row['apcdiv_staffname']; 

謝謝你們!

+0

'VAR object_row = object.1234B' – 2015-04-06 02:21:28

回答

1

你真的不需要這個jQuery。 JSON可以通過javascript直接訪問。

但是,如果你想使用jQuery -

var obj = jQuery.parseJSON('"1234A":{ 
    "apcdiv_staffname":"MOHD NASA", 
    "apcdiv_staffimage":"http://localhost/jknsapc/wp-content/uploads/2014/06/product-landing-layout-thumb.png", 
    "apcdiv_workplace":"HOSPITAL", 
    "apcdiv_grade":"U44", 
    "apcdiv_position":"PEGAWAI FARMASI", 
    "apcdiv_ic":"1234567" 
}'); 

alert(obj.1234A); 
alert(obj.1234A.apcdiv_staffname); 

更多信息

https://api.jquery.com/jquery.parsejson/

相關問題