Re: Struggling with Perl data structure for JSON object
[email protected] (Thomas Elsgaard) Sat, 10 Dec 2011 18:24:27 +0100
| Newsgroups | perl.copenhagen |
|---|---|
| Message-ID | <CAKSYKu+e4ga1t4nzYptCV62b0yKGPg=YtA1w9PsTCg-xz0kZaA@mail.gmail.com> |
On Sat, Dec 10, 2011 at 6:11 PM, Niels Jakob Darger <[email protected]> wrote: > I would use the excellent JSON::XS module. There is also the pure Perl > JSON::PP module (I've never used it but I would imagine it's a slower > drop-in replacement for JSON::XS). Alternatively you can use the JSON module > which loads whatever is on the system, preferring JSON::XS. > > use JSON::XS; > [...] > print encode_json($data->[0]{servers}); > > I wouldn't generate JSON myself, you'll eventually end up generating a > document which is not well-formed if there's any character (set) surprises > etc. The JSON modules handle everything - escaping, UTF-8 etc. > > Hilsen / regards, > Niels Jakob Darger 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