APP接口开发之PHP封装JSON数据 ,并读取JSON数据的方法

来源:互联网 发布:网络建设方案 编辑:程序博客网 时间:2024/06/07 12:06
<?php//服务端 app.php//php中用数组表示JSON格式数据header("Content-type:text/html;charset=utf-8");$arr = array(      'code' => 200,'msg' => '数据返回成功',    'data' => array(          'email' =>'fcwr@dibaye.com',          'website' =>'http://www.dibaye.com',  ),); echo json_encode($arr); //将数组封闭成JSON数据 主要函数json_encode;?>//客户端 list.php<?phpheader("Content-type:text/html;charset=utf-8");  $url = "http://localhost/syphp/app.php";  //URL取绝对路径   $tranlatestr = file_get_contents($url);  //获得URL文档 file_get_contents  $bb = json_decode($tranlatestr);  //将JSON数据转换成数组  echo $bb->data->website;//取得数组内容字段  ?>
</pre><p><strong><span style="font-size:24px; color:#ff0000">//用下面的方式存取方便实用:进阶了</span></strong></p><pre code_snippet_id="574976" snippet_file_name="blog_20150108_3_147348" name="code" class="php"><?php//服务端 app.php//php中用数组表示JSON格式数据header("Content-type:text/html;charset=utf-8");$arr = array(      'code' => 200,'msg' => '数据返回成功','date' =>array(       '0' => array(          'email' =>'1395591352@qq.com',          'website' =>'http://www.baidu.com',     ),   '1' => array(          'email' =>'564379992@qq.com',          'website' =>'http://www.sywlgzs.cn',     ),   '2' => array(          'email' =>'god1@126.com',          'website' =>'http://www.agewnet.com',     ),),); echo json_encode($arr); //将数组封闭成JSON数据 主要函数json_encode;?><?php//客户端list.phpheader("Content-type:text/html;charset=utf-8");  $url = "http://localhost/syphp/app.php";  //URL取绝对路径   $tranlatestr = file_get_contents($url);  //获得URL文档 file_get_contents  $bb = json_decode($tranlatestr);  //将JSON数据转换成数组  echo $bb->date[1]->website;//取得数组内容字段  ?>
1 0
原创粉丝点击