FAQ

[Catalyst] JSON encoding issue: array of hashes

Dan
Aug 29, 2010 at 7:13 am
Hi people :)

Catalyst beginner here. I'm trying to get an array of hashes encoded.
Something is going wrong.

My code:

---

sub getRetailers :Path :Args(1) {

my ( $self, $c ) = @_;

my $results = $c->model('ESDB::Retailer')->search_literal( '1=1' );
my @results;
foreach my $result ( $results->all ) {
push( @results, {
id => $result->retailerid,
label => $result->retailername
} );
};
$c->stash( @results );
$c->forward('EasySaver::View::JSON');

}

---

Output:

{"HASH(0x9f8bf88)":{"label":"AGL","id":"4"},"HASH(0xa4dbfb8)":{"label":"Integral","id":"2"}}

---

So obviously the HASH(memory location) stuff is wrong. And also I see 2
records there, id 2 and id 4. I'm missing id 1 and id 3.

Anyone know what I'm doing wrong?

Thanks :)

Dan
reply

Search Discussions

1 response

  • Jon Schutz at Aug 29, 2010 at 8:46 am
    Your line

    $c->stash( @results );

    is the problem. Something like

    $c->stash( results => \@results );

    is probably what you are looking for.

    It's all explained here:

    http://search.cpan.org/~bobtfish/Catalyst-Runtime-5.80025/lib/Catalyst.pm#$c-%3Estash

    --
    Jon Schutz My tech notes http://notes.jschutz.net
    Chief Technology Officer http://www.youramigo.com
    YourAmigo



    On 08/29/2010 04:43 PM, Dan wrote:
    Hi people :)

    Catalyst beginner here. I'm trying to get an array of hashes encoded.
    Something is going wrong.

    My code:

    ---

    sub getRetailers :Path :Args(1) {

    my ( $self, $c ) = @_;

    my $results = $c->model('ESDB::Retailer')->search_literal( '1=1' );
    my @results;
    foreach my $result ( $results->all ) {
    push( @results, {
    id => $result->retailerid,
    label => $result->retailername
    } );
    };
    $c->stash( @results );
    $c->forward('EasySaver::View::JSON');

    }

    ---

    Output:

    {"HASH(0x9f8bf88)":{"label":"AGL","id":"4"},"HASH(0xa4dbfb8)":{"label":"Integral","id":"2"}}

    ---

    So obviously the HASH(memory location) stuff is wrong. And also I see 2
    records there, id 2 and id 4. I'm missing id 1 and id 3.

    Anyone know what I'm doing wrong?

    Thanks :)

    Dan



    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/

Related Discussions

Discussion Navigation
viewthread | post

2 users in discussion

Dan: 1 post Jon Schutz: 1 post