mojo 接口返回键值对的json格式

  my $c = shift;
use DBI;
my %hash=();  
my $dbUser='zabbix';  
my $user="root";  
my $passwd="1234567"; 
my $dbh  = DBI->connect("dbi:mysql:database=$dbUser;host=192.168.32.xx;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
my $hostSql = qq{ select hostid,name  from hosts  where hostid in ('10001','10081','10050') };   
my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9); 
 
my $selStmt = $dbh->prepare($hostSql);  
$selStmt->execute();
$selStmt->bind_columns(undef, $a1, $a2);  
while( $selStmt->fetch() ){ 
print "$a1 is $a1
";
print "$a2 is $a2
";
$hash{$a1}=$a2;
  };
print %hash;
print "
";
  $c->render(json =>  \%hash );
  };

原文地址:https://www.cnblogs.com/hzcya1995/p/13350825.html