tags:

views:

44

answers:

2

I've read through a couple of dozen of these questions, but I can't find this solution. I want to output the key of a JSON key:value pair. Assuming the JSON is like this:

{"group":[
{"ed":"BDeveloper","st":"New","type":"B"},
{"ed":"BDeveloper","st":"Cancelled","type":"B"}
]}

I want to scrape the "ed", the "st", etc. out of the pair using jQuery if possible.

I currently have code around it to output the data:

$.getJSON('sum.php', function(JSONsum)
        {
        var summaryHTML ='';
        $.each(JSONsum.group, function (i)
            {
            console.log("this.ed: ["+i+"] "+this.ed);
            // console.log("this.name: ["+i+"] "+this.something??? );

            summaryHTML+='<div class=\"grid_3\">'+this.ed+'</div>';
            summaryHTML+='<div class=\"grid_1\">'+this.type+'</div>';
            summaryHTML+='<div class=\"grid_2\">'+this.st+'</div>';
            summaryHTML+='<div class=\"clear\"></div>';
            });
        $('#summary').append(summaryHTML);
        }); 

But I want to be able to output the key in the console.log and use it in other places, etc.

Is it even possible? Thanks in advance

+2  A: 

Change the fourth line to: $.each(JSONsum.group, function (i,e)

i is the key/index and e is the value. Adding a console.log(i+" : "+e); should do it. Take a look at the full documentation of each.

Kshitij Parajuli
Here's my output.sigh. 0 : [object Object]1 : [object Object]2 : [object Object]3 : [object Object]
Kevin Flavin
try `console.log(i + " : "); console.log(e);`
Kshitij Parajuli
That gives 0 :log: Object { ed="BDeveloper", more...}1 :Object { ed="BDeveloper", more...}2 :Object { ed="BDeveloper", more...}3 :log: Object { ed="BDeveloper", more...}
Kevin Flavin
sorry, I prematurely pressed enter. try the edited version?
Kshitij Parajuli
since `e` is another object, concatenating it with a string made it `[object Object]` in the first example. But if you log the object separately, it will log the contents of it. Is this the output you were looking for?
Kshitij Parajuli
Not exactly, I want to carve out a string that I can use that is the key, like 'ed'. I'll work on getting the string out of the object.
Kevin Flavin
You can do a `$.each(e,function(i_1,e_1){ // do sth with i_1 });` inside the each that is already there.
Kshitij Parajuli
A: 

Your code can look like following:

var jsonSum = {group:[
        {ed:"BDeveloper",st:"New",type:"B"},
        {ed:"BDeveloper",st:"Cancelled",type:"B"}
    ]},
    summaryHTML='',
    group=jsonSum.group;

for (var i=0, l=group.length; i<l; i++) {
    var obj=group[i];
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            if (typeof console !== "undefined") {
                console.log("this." + prop + "["+i+"]: '" + obj[prop] + "'");
            }
            summaryHTML += '<div class=\"grid_' + prop + '\">'+obj[prop]+'</div>';
        }
    }
    summaryHTML += '<div class=\"clear\"></div>';
}
$('#summary').append(summaryHTML);

I replaced the class names grid_1, grid_2, grid_3 to grid_ed, grid_st, grid_type. With the classes defined for example as

.grid_ed {float:left; background-color:red; width: 90px}
.grid_st {float:left; background-color:yellow; width: 90px; margin-left: 10px}
.grid_type {float:left; background-color:green; width: 90px; margin-left: 10px}
.clear {clear:left}

you will have the following results

alt text

and

alt text

in the log.

You can see this live here.

Oleg
Very nice! For the record, the grid_# represents the classes for use with the 960.gs grid system. I will use Id to capture the 'prop' and combine in a serialize. Clever. Thank you!
Kevin Flavin