简体   繁体   English

如何使用jQuery通过键搜索和获取JSON行数据?

[英]How to search and fetch json row data by key using jQuery?

I have this JSON object: 我有这个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"
},

I want to search and fetch json row data by using key. 我想使用键搜索和获取json行数据。 For example, if I search using key 1234B , it will return the data belong to 1234B key. 例如,如果我使用键1234B搜索,它将返回属于1234B键的数据。 From there I can proceed to grab the data inside such as apcdiv_staffname 从那里我可以继续获取里面的数据,例如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"

}

How to achieve it using jQuery? 如何使用jQuery实现它? I tried to Google but cannot find relevan answer, maybe because my choice of keyword. 我尝试使用Google,但找不到答案,可能是因为我选择了关键字。 If using PHP array, I can simply using this code: 如果使用PHP数组,我可以简单地使用以下代码:

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

Thanks guys! 多谢你们!

You really don't need jQuery for this. 您真的不需要jQuery。 JSON can be accessed directly through javascript. JSON可以直接通过javascript访问。

However if you want to use jQuery - 但是,如果您想使用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 );

More Information 更多信息

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM