user3444889
user3444889

Reputation: 1

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

Upvotes: 0

Views: 37

Answers (3)

Ankit Tyagi
Ankit Tyagi

Reputation: 2375

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
}

Upvotes: 2

N K
N K

Reputation: 3337

Object.keys(jsonObject) is enough.

Upvotes: 0

Kiran
Kiran

Reputation: 20293

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

Upvotes: 1

Related Questions