2

I want to get the key from the JSON Array? This is my JSON Array

****JAVASCRIPT****
var employees = [
                 { "firstName":"John" , "lastName":"Doe" }, 
                 { "firstName":"Anna" , "lastName":"Smith" }, 
                 { "firstName":"Peter" , "lastName": "Jones" }
               ];

From this JSON I need the result key. I mean "firstName", "lastName", not John, Anna, Peter Please help to get the keys from JSON.

5
  • 3
    there are 3 firstname and lastname there which one do you want Commented Nov 5, 2013 at 11:01
  • 3
    employees[0].firstname will return John, employees[1].firstname will return Anna and so on. Commented Nov 5, 2013 at 11:02
  • 1
    no. I want to have a solution "firstName", "lastName". also. not John,Anna,Peter Commented Nov 5, 2013 at 11:05
  • 1
    He wants this. Commented Nov 5, 2013 at 11:06
  • 1
    @user2894088: state your question more clearly Commented Nov 5, 2013 at 11:08

5 Answers 5

4

Try this:

http://jsfiddle.net/NJMyD/1072/

var employees = '[{"firstName":"John","lastName":"Doe" },{"firstName":"Anna","lastName":"Smith" },{"firstName":"Peter","lastName":"Jones"}]';

var myData = JSON.parse(employees);

$(document).ready(function() {
    $.each(myData, function() {
        $('<li>' + this.firstName + ' ' + this.lastName + '</li>').appendTo("#groups");
    });
});
Sign up to request clarification or add additional context in comments.

Comments

2

If you are saving javascript in external file, save as filename.json

var employees = [
                 { "firstName":"John" , "lastName":"Doe" }, 
                 { "firstName":"Anna" , "lastName":"Smith" }, 
                 { "firstName":"Peter" , "lastName": "Jones" }
               ];

Then improt filename.json in webpage using jquery as //jquery plugin must be added first

$(document).ready(function(){
$.getJSON("filename.json",function(result){
alert(result[0].firstname + result[0].lastname);
});
});

Comments

1

Try this

var employees = [
             { "firstName":"John" , "lastName":"Doe" }, 
             { "firstName":"Anna" , "lastName":"Smith" }, 
             { "firstName":"Peter" , "lastName": "Jones" }
           ]; //your array

for(var i in employees)
{
var fname=employees[i].firstName
var lname=employees[i].lastName
console.log(fname) //all first names
console.log(lname) //all last names
}

Fiddle

http://jsfiddle.net/8TB6Z/

1 Comment

no. I want to have a solution "firstName", "lastName". also. not John,Anna,Peter
1

Try this:

var employees = [
    { "firstName":"John" , "lastName":"Doe" }, 
    { "firstName":"Anna" , "lastName":"Smith" }, 
    { "firstName":"Peter" , "lastName": "Jones" }
];
$.each(employees,function(f,n){                    
    $.each(n,function(ff,nn){
        console.log("field:"+ff+" value:"+nn);                        
    });
}); 

Fiddle here:

Hope it helps you.

2 Comments

but I see many times "firstName" and "lastName". How to show only one time?
@user2894088 you see it 3 times because you have 3 objects. if you have same field names across your array employees, you can do it just 1 time. See here.
1

I think you need the key. Object has a prototype keys which returns an Array of keys in an Object

Object.keys(employees); // returns ["firstName", "lastName"]

Also

for(i in employees){
var key = i;
var val = employees[i];
for(j in val)
    {
    var sub_key = j;
    var sub_val = val.j;
    console.log(sub_key);
    }
}

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.