1

When I do

$.each(result, function(i, n){
alert("key: " + i + ", Value: " + n );
});

then for each iteration I see

key: 276, Value: {"owners":["he"],"users":["he","m"],"end":"07/06-2011","groups":[],"type":"in"}

How do I access the values of owners, users, end, groups, and type for each iteration?

In Perl would I have done

foreach my $key (keys %result) {
   print $result{$key}{owners};
   print $result{$key}{users};
   ...
}

Update

I get result from JSON like so

$.ajax({
    type: "GET",
    url: "/cgi-bin/ajax.pl",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: { "cwis" : id },
    // ...
    success: function(result){
    if (result.error) {
        alert('result.error: ' + result.error);
    } else {

        $.each(result, function(i, n){
        alert( "key: " + i + ", Value: " + n );

        });


    }
    }
});

Update 2

It seams that the problem is the server side is not sending prober JSON.

This is the server side script that generate the JSON string.

!/usr/bin/perl -T

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use JSON;
use utf8;
use strict;
use warnings;

my $cgi = CGI->new;
$cgi->charset('UTF-8');

my $json_string = qq{{"error" : "The user do not have any activities."}};

my $json = JSON->new->allow_nonref;
$json = $json->utf8;

# @a and $act is now available

my $data;
foreach my $id (@a) {
    $data->{$id} = $json->encode(\%{$act->{$id}});
}
$json_string = to_json($data);


print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_string;
Sandra Schlichting
  • 22,478
  • 28
  • 91
  • 145
  • can you also show the result that the perl script generates ? – Teneff Jun 23 '11 at 12:36
  • 1
    I'm no CGI expert but it looks like you are double encoding your data into JSON. Once with `my $json = JSON->new->allow_nonref; $json = $json->utf8;` and then again with `$data->{$id} = $json->encode(\%{$act->{$id}})` . Can't say I've ever tried to do that before! – T9b Jun 23 '11 at 12:37
  • @T9b : You are exactly right! That was the problem =) If you post is as a solution, then I accept it =) Thanks a lot =) – Sandra Schlichting Jun 23 '11 at 13:11
  • @ Sandra Schlichting - I'll update my answer below. – T9b Jun 23 '11 at 21:31

4 Answers4

2

in $.each callbacks, this points to the current element, so

$.each(result, function(i, n){
     alert(this.users);
});
user187291
  • 50,823
  • 18
  • 89
  • 127
2
document.write(result[key].owners);
document.write(result[key].users);

UPDATE:

Apparently my comment on the question was the answer:

I'm no CGI expert but it looks like you are double encoding your data into JSON. Once with

my $json = JSON->new->allow_nonref; $json = $json->utf8; 

and then again with

$data->{$id} = $json->encode(\%{$act->{$id}}) .

T9b
  • 2,576
  • 4
  • 26
  • 45
  • are you by any chance trying to create JSON in the values? Are you sure it's been formated correctly, and then parsed correctly? Are you generating it manually or is it received from some feed? – T9b Jun 23 '11 at 11:50
  • `result` comes from JSON, so I am trying to access data that have been JSON decoded. In 1 minute will I have updated the question with the Ajax call. – Sandra Schlichting Jun 23 '11 at 11:54
  • Can you change it to `alert( "key: " + i + ", Type: " + (typeof n) );` and tell us the results? It certainly looks like the JSON is not being parsed properly somehow. – Björn Jun 23 '11 at 12:15
  • @Björn : It says it is string. In a minutes time will I post the servide side script. – Sandra Schlichting Jun 23 '11 at 12:22
1
n.owners or n['owners']
n.users or n['users']
etc.

In a loop...

$.each(result, function(k,v) {
    console.log("key: " + k + ", value: " + v );
    $.each(v, function(k,v)) {
        console.log("key: " + k + ", value: " + v );
    });
});
ninjagecko
  • 77,349
  • 22
  • 129
  • 137
1

you can access them like this:

n.owners

or

n['owners']

or you can use another cycle :

$.each(result, function(i, n){
    if (typeof(n)=='object') {
        $.each(n, function(k, v){
            alert('n.'+k+' = ' + v);
        });
    }
});

edit: jsFiddle Example Example 2

edit2: to avoid getting undefined make a simple check whether the key i is equal to "Value", so it's value will be an object

Teneff
  • 23,912
  • 8
  • 52
  • 85
  • okay, this is very interesting. I am getting `is not an object` when I use your code from jsFiddle on my data. – Sandra Schlichting Jun 23 '11 at 12:08
  • @Sandra just `"is ..."` ... no key name ? – Teneff Jun 23 '11 at 12:11
  • When I use your code from example 2 on my data, it always enters your `else`-condition. – Sandra Schlichting Jun 23 '11 at 12:11
  • @Sandra try `alert(typeof(n));` and check what `n` actually is – Teneff Jun 23 '11 at 12:13
  • @Teneff : It returns string. So I guess the problem is on the server side then? – Sandra Schlichting Jun 23 '11 at 12:20
  • @Teneff your answer works for the first field. It doesn't for the second and the third field returns undefined. After that all fields are errored @Sandra Schlichting - the reason I asked if you were using JSON is that I'm pretty sure it's not encoded properly. I'm not sure what your square brackets are supposed to achieve. Check the question I set here http://stackoverflow.com/questions/6194180/json-how-find-another-value-at-the-same-index-from-a-value-in-javascript-object – T9b Jun 23 '11 at 12:25
  • @Sandra if it's a `string` you could try `n=eval(n);` – Teneff Jun 23 '11 at 12:26
  • @Teneff : When I try `n=eval(n); alert(n);` I get `invalid label` in the console. – Sandra Schlichting Jun 23 '11 at 12:35
  • @Teneff : Yes, it is temping to believe that the problem is in the server side script based on all this. I have posted the script, and am looking into right now – Sandra Schlichting Jun 23 '11 at 12:36