Converting array of numbers to json in perl, getting array of strings

前端 未结 3 1096
有刺的猬
有刺的猬 2021-01-19 11:12

I\'m trying to convert an array of numbers to json:


   $json = to_json(\"numbers_array\" => \\@numbers_array);
   print $json;

but the output I\'m get

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-19 11:25

    From the JSON documentation:


    You can force the type to be a number by numifying it:

       my $x = "3"; # some variable containing a string
       $x += 0;     # numify it, ensuring it will be dumped as a number
       $x *= 1;     # same thing, the choice is yours.
    

    I would try something like this:

        $json = to_json({numbers_array => [map { $_ + 0 } @numbers_array]});
        print $json;
    

提交回复
热议问题