简体   繁体   中英

Access to a certain part of JSON data with jQuery

So i have this JSON:

{
  "id_u":"1",
  "nombre_usuario":"JESUS",
  "apellido_paterno_usuario":"DIAZ"
}

I just want to access to the fields names

id_u,_nombre_usuario,apellido_paterno_usuario

And then, create an array with that info.

How can i do that?

Thanks guys

Do this way :

var keyValuePair = {
  "id_u":"1",
  "nombre_usuario":"JESUS",
  "apellido_paterno_usuario":"DIAZ"
};

var arr =new Array();
for (key in keyValuePair){
arr.push(key); // for keys
// arr.push(keyValuePair[key]); // for values
}

Use .each() to parse JSON.

Try this:

var keyArray = [];
var a = {
  "id_u":"1",
  "nombre_usuario":"JESUS",
  "apellido_paterno_usuario":"DIAZ"
};

$.each(a,function(i,v){
    keyArray.push(i); // i is json key and v is json value.
});
console.log(keyArray);

DEMO

Object.keys(jsonObject)就足够了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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