How to fill list with JSON data?
Here's my code:
my $groups = get_groups($t);
my @group;
my $i = 0;
do {
push(@group, {
groups => [
{ type => $groups->{groups}->[$i]->{type} , group => $groups->{groups}->[$i]->{group} },
]
});
$i++;
} while ($i < length $groups->{groups});
Here is the json sample:
{
"error":false,
"message":"success",
"group":[
{"type":1,"group":"group1"},
{"type":2,"group":"group2"},
{"type":3,"group":"group3"},
{"type":4,"group":"group4"},
{"type":5,"group":"group5"}
]
}
Function get_groups($t); will return above json. I want to get the array group and put it into list groups. But I got:
Can't use string ("0") as a HASH ref while "strict refs" in use
do ... whileconstruct, it's quite un-Perlish and hard to read. And there's no more JSON in this. JSON is irrelevant now. It's now just data.length $opsteams->{teams}is probably not what you want. That returns the character length of a string, not the element length of an array. Tryscalar @{$opstreams->{teams}}