将哈希数组转换为 json [英] Convert array of hashes to json
本文介绍了将哈希数组转换为 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文