Re: Struggling with Perl data structure for JSON object
[email protected] (Niels Jakob Darger) Sat, 10 Dec 2011 18:38:36 +0100
| Newsgroups | perl.copenhagen |
|---|---|
| Message-ID | <[email protected]> |
Ah, I see. How about something along the lines of:
my @servers;
while (my $ref = $sth->fetchrow_hashref()) {
push @servers, $ref;
}
print encode_json(\@servers);
Or, if you need the full structure (your example suggests not):
my $data = [
{
servers => []
}
];
while (my $ref = $sth->fetchrow_hashref()) {
push @{$data->[0]{servers}}, $ref;
}
Completely untested, I don't have a Perl/DBI setup at hand...
Jakob
> Hi Jakob
>
> Yes, i will use a dedicated JSON module for converting the data
> structure into the JSON object, but my main problem is to create the
> perl data structure, in order to encode it with JSON:XS:
>
> my $data = [
> {
> 'servers' => [
> {
> 'DateTime' => '2011-11-13 13:04:00',
> 'TotalNumberOfUsersRegistered' => '1205.00'
> },
> {
> 'DateTime' => '2011-11-13 13:00:00',
> 'TotalNumberOfUsersRegistered' => '1205.00'
> }
> ]
> }
> ];
>
> ///Thomas