博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
接口返回json
阅读量:5239 次
发布时间:2019-06-14

本文共 2309 字,大约阅读时间需要 7 分钟。

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 ~]#

转载于:https://www.cnblogs.com/zhaoyangjian724/p/6200257.html

你可能感兴趣的文章
水平垂直居中
查看>>
MySQL简介
查看>>
设计模式之桥接模式(Bridge)
查看>>
jquery的$(document).ready()和onload的加载顺序
查看>>
Python Web框架Django (五)
查看>>
.net学习之继承、里氏替换原则LSP、虚方法、多态、抽象类、Equals方法、接口、装箱拆箱、字符串------(转)...
查看>>
【codevs1033】 蚯蚓的游戏问题
查看>>
【程序执行原理】
查看>>
python的多行注释
查看>>
连接Oracle需要jar包和javadoc文档的下载
查看>>
UVA 10976 - Fractions Again?!
查看>>
Dreamweaver cc新版本css单行显示
查看>>
【android】安卓的权限提示及版本相关
查看>>
JavaScript可否多线程? 深入理解JavaScript定时机制
查看>>
IOS基础学习
查看>>
PHP 导出 Excell
查看>>
Java基础教程——网络基础知识
查看>>
Kruskal基础最小生成树
查看>>
ubuntu 14.04 安装搜狗拼音输入法
查看>>
浅谈算法和数据结构: 一 栈和队列
查看>>