0

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

1
  • did you try object["apellido_paterno_usuario"] Commented Mar 21, 2014 at 4:19

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it is working but i dont understand the code. What does it mean the "K in A" ?
We just fetching every key, we can write "key in a"
@user3444889, for(key in keyValuePair) {}
1

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

Comments

0

Object.keys(jsonObject) is enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.