将哈希数组转换为 json

编程入门 行业动态 更新时间:2024-10-28 16:17:39
本文介绍了将哈希数组转换为 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想转换我这样创建的哈希数组:

I want to convert an array of hashes that I create like this:

while(...) { ... push(@ranks, {id => $id, time => $time}); }

到 JSON:

use JSON; $j = new JSON; print $j->encode_json({ranks => @ranks});

但它输出的是:

{"ranks":{"time":"3","id":"tiago"}, "HASH(0x905bf70)":{"time":"10","id":"bla"}}

如您所见,它无法写入散列并且没有数组...

As you can see it isnt able to write on of the hashes and there's no array...

我想输出如下所示的 JSON 字符串:

I would like to output a JSON string that looked like this:

{"ranks":[{"time":"3","id":"tiago"}, {"time":"40","id":"fhddhf"}, {"time":"10","id":"bla"}]}

推荐答案

print $j->encode_json({ranks => @ranks});

应该是:

print $j->encode_json({ranks => \@ranks});

更多推荐

将哈希数组转换为 json

本文发布于:2023-10-28 23:00:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   将哈希   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!