加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 06:06:21 所属栏目:大数据 来源:网络整理
导读:我正在尝试将数组转换为json: $json = to_json("numbers_array" = @numbers_array); print $json; 但我得到的输出是: {"numbers_array" = ["1","2","3"] } 我正在使用push()添加元素来构建数组.如何转换为json并保留数字类型,以便输出为: {"numbers_arra
我正在尝试将数组转换为json:


   $json = to_json("numbers_array" => @numbers_array);
   print $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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读