• 接口返回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 ~]# 
    

  • 相关阅读:
    Python Data Analysis Library¶
    matadon/mizuno
    MySQL 5.1参考手册
    Apache Derby: Quick Start
    JRuby大捷:ThoughtWorks宣布Mingle发布在即
    RailsWithH2InJNDIOnJetty
    Embedded Jetty and Spring: The Lightweight Antidote for Java EE Complexity
    window下安装解压缩版mysql/zip压缩文件包非install安装程序
    Chapter 9. Extending Workbench
    Mysql手动增加一列_Blog of Grow_百度空间
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6200256.html
Copyright © 2020-2023  润新知