接口返回json

use Mojolicious::Lite;
use JSON qw/encode_json decode_json/;  

# /foo?user=sri
 get '/api' => sub {
   my $c = shift;
     my $env = $c->param('env');
      
     if ($env){
              use DBI;
my $dbName = 'oadb';  
my $dbUser = 'query';  
my $dbUserPass = 'xxxxxx';
my @arr2="";  
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database " ;
my $hostSql = qq{select  IP,INFO,ENV from  machine_info  where ENV='env-$env' }; 
my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);  
my $selStmt = $dbh->prepare($hostSql);  
$selStmt->bind_columns( \$a1, \$a2, \$a3);  
$selStmt->execute();  
while( $selStmt->fetch() ){ push (@arr2, "$a1  $a2  $a3" );

       };
     my   $arr2=\@arr2;
     my $json =encode_json($arr2);  
       $c->render(text => "$json");
};
};

       app->start;
	   
这个接口返回的是:

[root@dr-mysql01 ~]# perl a1.pl 
["","192.168.32.111  haproxy  env-zjtest2","192.168.32.178  front-end  env-zjtest2","192.168.32.180  backoffice  env-zjtest2","192.168.32.181  app  env-zjtest2","192.168.32.185  zjzc-mysql  env-zjtest2","192.168.32.186  pay-mysql  env-zjtest2","192.168.32.187  redis  env-zjtest2","192.168.32.182  nfs  env-zjtest2"]



修改接口:

use Mojolicious::Lite;
use JSON qw/encode_json decode_json/;  

# /foo?user=sri
 get '/api' => sub {
   my $c = shift;
     my $env = $c->param('env');
      
     if ($env){
              use DBI;
my $dbName = 'oadb';  
my $dbUser = 'query';  
my $dbUserPass = 'kjk7787czcb';
my @arr2="";  
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database " ;
my $hostSql = qq{select  IP,INFO,ENV from  machine_info  where ENV='env-$env' }; 
my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);  
my $selStmt = $dbh->prepare($hostSql);  
$selStmt->bind_columns( \$a1, \$a2, \$a3);  
$selStmt->execute();  
while( $selStmt->fetch() ){ push (@arr2, "$a1  $a2  $a3" );

       };
         $c->render(json => \@arr2);
};
};

       app->start;
	   
返回JSON:
	   

["","192.168.32.111  haproxy  env-zjtest2","192.168.32.178  front-end  env-zjtest2","192.168.32.180  backoffice  env-zjtest2","192.168.32.181  app  env-zjtest2","192.168.32.185  zjzc-mysql  env-zjtest2","192.168.32.186  pay-mysql  env-zjtest2","192.168.32.187  redis  env-zjtest2","192.168.32.182  nfs  env-zjtest2"][root@dr-mysql01 ~]# 

你可能感兴趣的:(接口返回json)