<?php //聲明header為json header("Content-type:application/json"); //構建數(shù)據(jù) $data = array( array( 'title'=>'百度', 'time'=>'2000', 'website'=>'www.baidu.com' ), array( 'title'=>'阿里巴巴', 'time'=>'1999', 'website'=>'www.alibaba.com' ), array( 'title'=>'騰訊', 'time'=>'1998', 'website'=>'www.qq.com' ) ); //轉為JSON echo json_encode($data,JSON_UNESCAPED_UNICODE); ?>
返回的格式
[ { "title":"百度", "time":"2000", "website":"www.baidu.com" }, { "title":"阿里巴巴", "time":"1999", "website":"www.alibaba.com" }, { "title":"騰訊", "time":"1998", "website":"www.qq.com" } ]
<?php //聲明header為json header("Content-type:application/json"); //構建數(shù)據(jù) $data = array( 'title'=>'百度', 'time'=>'2000', 'website'=>'www.baidu.com' ); //轉為JSON echo json_encode($data,JSON_UNESCAPED_UNICODE); ?>
返回的格式
{ "title":"百度", "time":"2000", "website":"www.baidu.com" }
<?php //聲明header為json header("Content-type:application/json"); //構建數(shù)據(jù) $data = array( 'title'=>'阿里巴巴', 'time'=>'1999', 'website'=>'www.alibaba.com', 'msg'=>array( 'originator'=>'馬云', 'price'=>'5842.70億美元', 'sstime'=>'2014年9月19日' ) ); //轉為JSON echo json_encode($data,JSON_UNESCAPED_UNICODE); ?>
返回的格式
{ "title":"阿里巴巴", "time":"1999", "website":"www.alibaba.com", "msg":{ "originator":"馬云", "price":"5842.70億美元", "sstime":"2014年9月19日" } }
<?php //聲明header為json header("Content-type:application/json"); //構建數(shù)據(jù) $data = array( 'title'=>'阿里巴巴', 'time'=>'1999', 'website'=>'www.alibaba.com', 'msg'=>array( array( 'product'=>'淘寶', 'url'=>'https://www.taobao.com' ), array( 'product'=>'天貓', 'url'=>'https://www.tmall.com' ), array( 'product'=>'1688', 'url'=>'https://www.1688.com' ) ) ); //轉為JSON echo json_encode($data,JSON_UNESCAPED_UNICODE); ?>
返回的格式
{ "title":"阿里巴巴", "time":"1999", "website":"www.alibaba.com", "msg":[ { "product":"淘寶", "url":"https://www.taobao.com" }, { "product":"天貓", "url":"https://www.tmall.com" }, { "product":"qydn", "url":"https://www.qydn.com" } ] }