Your code seems to be missing some significant chunks, so let's add in the missing bits (I'll make some assumptions here) and fix things as we go.
Add missing boilerplate.
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
my $name = "test";
my $type = "A";
my $data = "1.1.1.1";
my $ttl = 84600;
Make the hash a hash and not an array and don't forget to localise it: my %
my %rec_hash = ('name'=>$name, 'type'=>$type,'data'=>$data,'ttl'=>$ttl);
实际使用encode_json
方法(传递一个hashref):
my $json = encode_json \%rec_hash;
输出结果:
print $json;
这就像我预期的那样工作正常,没有任何错误.