php解析复杂json的实例 - 火车api的应用

首页 - 技术杂谈 本文发布于2016-10-28 16:01:00

假设有火车车次接口:http://www.guabu.com/api/checi.php?checi=T110

json代码为:

{"ticketInfo":{"T110":[{"pr":257.5,"type":"硬座"},{"pr":405.5,"type":"硬卧"},{"pr":577.5,"type":"软卧"},{"pr":1074,"type":"高级软卧"},{"pr":257.5,"type":"无座"}]},"trainInfo":{"T110":{"arriCity_py":"beijing","dptDate":"2016-10-28","code":"T110","status":"1","arriTime":"09:30","istmp":false,"deptCity_py":"shanghai","arrDate":"2016-10-29","arriCity":"北京","trainType":"空调特快","deptTime":"18:02","arriStation":"北京","deptStation":"上海","deptCity":"上海","interval":"15小时28分钟"}},"trainScheduleHead":["站次","站名","日期","到达时间","开车时间","停车时间","里程","硬座","硬卧下","软卧下"],"extInfo":{"allMileage":"1463公里","allTime":"15小时28分钟"},"trainScheduleBody":[{"mxl":12490639969101,"content":[34,"徐州","第二天","01:35","01:42","7分","649公里",119,"182","253"]},{"content":[17,"无锡","第一天","19:19","19:23","4分","126公里",33.5,"73.5","105.5"]},{"mxl":7644416122782,"content":[13,"沧州","第二天","06:45","06:55","10分","1199公里",153.5,"272.5","423"]},{"content":[76,"苏州","第一天","18:51","18:54","3分","84公里",88.5,"68.5","96.5"]},{"content":[55,"常州","第一天","19:46","19:50","4分","165公里",75.5,"82","114.5"]},{"content":[38,"上海","-","起点站","18:02","-",0,37,"0","0"]},{"content":[45,"北京","第二天","09:30","终到站","-","1463公里",212.5,"325.5","497.5"]},{"content":[72,"德州","第二天","05:49","05:51","2分","1086公里",203.5,"275","395"]},{"content":[93,"南京","第一天","21:50","22:01","11分","301公里",134.5,"107","149"]},{"content":[51,"天津西","第二天","08:05","08:09","4分","1315公里",205.5,"295","465"]}]}

用php如何解析出各参数值:

checi_json.php代码如下:

<?php
header('Content-Type: text/html; charset=utf-8');
//方法一和方法二执行结果是一样的 from guabu
//$url = 'http://www.guabu.com/api/checi.php?checi=$checi';	//方法一
//$obj = file_get_contents($url);	//方法一
//echo ("要解析的json:".$obj."<br>");		//方法一
//$obj = json_decode($obj);	//方法一

$checi = "G5";
$url = fopen("http://www.guabu.com/api/checi.php?checi=$checi","rb");    //rb表示读取二进制文件,方法二
while (!feof($url)) {  //feof() 函数检测是否已到达文件末尾,方法二
    $obj .= fread($url, 10000);    //fread() 从文件里 读取最多 xx 个字节,方法二
}	//,方法二
fclose($url); //方法二
 
$obj = json_decode($obj);    //json_decode表示对获取到的JSON格式的字符串进行解码
$ticket = $obj->ticketInfo;
$trainInfo = $obj->trainInfo;
$trainScheduleBody = $obj->trainScheduleBody;

echo '循环数组次数:'.count($ticket->$checi).'<br>';
foreach ($ticket->$checi as $key1) {    //对应 json 里 ticketInfo 参数
  echo '<li>pr = '.$key1->pr.' , type = '.$key1->type.'</li>';
}

echo '<br>打印出trainInfo下的车次各对应参数值<br>';	
if(count($obj->trainInfo->$checi)){	//echo count($obj->trainInfo->$checi); 如果存在trainInfo的$checi值则执行如下
print 'arriCity_py值:'.$obj->{'trainInfo'}->{'$checi'}->{'arriCity_py'}.'<br>';
print 'dptDate值:'.$obj->{'trainInfo'}->{'$checi'}->{'dptDate'}.'<br>';
print 'code值:'.$obj->{'trainInfo'}->{'$checi'}->{'code'}.'<br>';
print 'status值:'.$obj->trainInfo->$checi->status.'<br>';
print 'arriTime值:'.$obj->trainInfo->$checi->arriTime.'<br>';
print 'istmp值:'.$obj->trainInfo->$checi->istmp.'<br>';
print 'deptCity_py值:'.$obj->trainInfo->$checi->deptCity_py.'<br>';
print 'arrDate值:'.$obj->trainInfo->$checi->arrDate.'<br>';
print 'arriCity值:'.$obj->trainInfo->$checi->arriCity.'<br>';
print 'trainType值:'.$obj->trainInfo->$checi->trainType.'<br>';
print 'deptTime值:'.$obj->trainInfo->$checi->deptTime.'<br>';
print 'arriStation值:'.$obj->trainInfo->$checi->arriStation.'<br>';
print 'deptStation值:'.$obj->trainInfo->$checi->deptStation.'<br>';
print 'deptCity值:'.$obj->trainInfo->$checi->deptCity.'<br>';
print 'interval值:'.$obj->trainInfo->$checi->interval.'<br>';
}

echo '<br>打印出trainScheduleHead的各内容<br>';	
if(count($obj->trainScheduleHead)){	//处理trainScheduleHead节点
foreach ($obj->trainScheduleHead as $key2) {    //对应 json 里 trainScheduleHead里的内容
print $key2.'<br>';
}
}
echo '<br>打印出extInfo的内容<br>';	
if(count($obj->extInfo)){	//处理extInfo节点
print 'allMileage值:'.$obj->extInfo->allMileage.'<br>';
print 'allTime值:'.$obj->extInfo->allTime.'<br>';
}

echo '<br>循环打印出trainScheduleBody的各内容<br>';	
if(count($obj->trainScheduleBody)){	//处理trainScheduleBody节点
foreach ($obj->trainScheduleBody as $key3) {    //对应 json 里 trainScheduleHead里的内容
foreach ($key3->content as $key4) {    //处理循环的content内容
print $key4.'<br>';
}
}

}

?>

参考运行例子:

http://www.guabu.com/jishuzatan/phpschool/json_complex.php


首页 | 吉凶大全 | 星座配对 | 塔罗牌

提示:本站不提供技术解答服务