\@numbers_array); 打印 $json; 但我得到的输出是: {"numbers-6ren">
gpt4 book ai didi

json - 在perl中将数字数组转换为json,获取字符串数组

转载 作者:行者123 更新时间:2023-12-01 09:29:10 24 4
gpt4 key购买 nike

我正在尝试将数字数组转换为 json:

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

但我得到的输出是:

{"numbers_array"=> ["1","2","3"] }

我正在通过使用 push() 添加元素来构建数组。如何转换为 json 并保留数字类型,以便输出为:

{"numbers_array"=> [1,2,3]}

谢谢!

最佳答案

来自 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.

我会尝试这样的事情:
    $json = to_json({numbers_array => [map { $_ + 0 } @numbers_array]});
print $json;

关于json - 在perl中将数字数组转换为json,获取字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17958476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com