php调用json输出某个字段的两种方法

$a = '{"status":"3","message":"","errCode":"0","data":[{"time":"2014-12-12 20:37","context":"到达:湖南湘潭公司 已收件"},{"time":"2014-12-12 21:31","context":"到达:湖南湘潭公司 发往:福建厦门分拨中心"},{"time":"2014-12-13 02:24","context":"到达:湖南长沙分拨中心"},{"time":"2014-12-17 20:02","context":"到达:福建厦门公司国贸分部 发往:福建厦门公司国贸分部"},{"time":"2014-12-17 20:33","context":"到达:福建厦门公司国贸分部 由 图片 签收"}],"html":"","mailNo":"1201519497579","expTextName":"韵达快递","expSpellName":"yunda","update":"1420006818","cache":"0","ord":"ASC","tel":"021-39207888"}';

第一种:

$b = json_decode($a);//对象
foreach($b->data as $v){
    $message .= $v->time.'  '.$v->context."\r\n";
}
echo $message;

第二种:

$b = json_decode($a,true);//数组
foreach($b['data'] as $v){
    $message .= $v['time'].'  '.$v['context']."\r\n";
}
echo $message;

结果:

2014-12-12 20:37 到达:湖南湘潭公司 已收件
2014-12-12 21:31 到达:湖南湘潭公司 发往:福建厦门分拨中心
2014-12-13 02:24 到达:湖南长沙分拨中心
2014-12-17 20:02 到达:福建厦门公司国贸分部 发往:福建厦门公司国贸分部
2014-12-17 20:33 到达:福建厦门公司国贸分部 由 图片 签收

评论已关闭