简体   繁体   中英

JSON:PP only encodes first item in array

I'm pushing my values into the array...

while ( ... ) {
  push @array, { label => "label", value => "value" };
}

This appears to be working.

Then...

use JSON::PP ;
print JSON::PP->new->utf8->encode(@array) ; 

only generates...

{"value":"value","label":"label"}

but I need...

[{"value":"value","label":"label"}{"value":"value","label":"label"} etc.. ]

(each item in array outputted, not just the first one...)

Any ideas?

Try passing a reference to the array:

print JSON::PP->new->utf8->encode(\@array) ; 

Encode is documented to take a scalar, not an array (so you need the reference, which is a scalar).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM