我想打印 JSON 对象的所有键值对。我不知道对象的键,所以我试图在一个循环中获取所有键和相应的值。但似乎我遗漏了一些明显的东西。
我的 perl 代码
%some_data = ("key1" => "value1","key2" => "value2","key3" => "value3","key4" => "value4");
my $json = encode_json \%some_data;
print $json; # it prints {"key2":"value2","key4":"value4","key1":"value1","key3":"value3"}
我简单的 javascript 代码
var jsonObj=$json;
var keys= Object.keys(jsonObj);
for (var i = 0; i < keys.length; i++){
document.write("<br /> ");
document.write(keys[i]);
// document.write(jsonObj.[keys[i]]); # doesnt work
}
document.write(jsonObj.key1); #works