[dancer-users] ajax plugin, serialize a 1D perl array to JSON
Rik Brown
rik at rikbrown.co.uk
Sun Jul 7 01:59:21 BST 2013
{ @sendArray } won't do what you want because you're putting the array inside a hash reference.
You either want to put it inside an array ref, or reference it, e.g.
[
@sendArray
]
or just \@sendArray.
Of course you could also do:
{
results => \@sendArray
}
if you want your output to be a hash at the top level.
Hope this helps!
--
Rik Brown
http://www.rikbrown.co.uk/
On Saturday, 6 July 2013 at 16:28, Anand Meher wrote:
> Dear dancer experts,
> I have a 1D Perl array which I would like to encode using JSON and send it as a response to an ajax request.
>
> I have implemented the following steps so far in my dancer app:
>
> use Dancer::Plugin::Ajax;
> set serializer => 'JSON';
>
> ajax '/getarray' => sub {
>
> my @sendArray;
> ...........
> {
> @sendArray; # this is the array I intend to send to the client Java Script
> }
>
> };
>
>
> The sendArray is not a (key, value) pair based structure, but a simple 1D array.
> Can anyone let me know if "@sendArray" statement at the end of ajax routine above will correctly encode to JSON?
>
> Also is there any way to print (to console) the final encoded JSON object from the ajax routine at the server end?
>
> thanks in advance
> \amk
>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm (mailto:dancer-users at dancer.pm)
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20130706/e01159b3/attachment.htm>
More information about the dancer-users
mailing list