PHP 怎么解析远程URL 传过来json ,在前端显示出来

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 怎么解析远程URL 传过来json ,在前端显示出来相关的知识,希望对你有一定的参考价值。

json 数据 :
"ErrorCode":0,"Success":true,"ErrMessage":"","HasData":true,"Records":["Time":"2014-08-19 07:47:10","Location":"广东省河源市G25黄田至义合高速路段","Reason":"驾驶中型以上载客载货汽车、危险物品运输车辆以外的其他机动车行驶超过规定时速10%未达20%的","count":"150","status":"0","department":"河源市公安局交通警察支队高速公路二大队","Degree":"3","Code":"1352A","Archive":"4416307900262638","Telephone":null,"Excutelocation":null,"Excutedepartment":null,"Category":"","Latefine":"0","Punishmentaccording":null,"Illegalentry":null,"Locationid":4416,"LocationName":"广东河源","DataSourceID":9992,"RecordType":"实时数据","Poundage":"25","CanProcess":"1","UniqueCode":"fbbe4d88558cff1f25a8770a42cd4e3e","SecondaryUniqueCode":"1993287","DegreePoundage":"0","Other":"","CanprocessMsg":"","CooperPoundge":"25","ActivePoundge":"-1","CanUsePackage":"0"],"ResultType":"实时数据","LastSearchTime":"","PackageName":"","RemainCount":0,"Other":""

我想 在php 中 接收 这个 json 数据 并在前端显示出来。

比如:
$jsonData = '"ErrorCode":0,"Success":true,"ErrMessage":"","HasData":true,"Records":["Time":"2014-08-19 07:47:10","Location":"广东省河源市G25黄田至义合高速路段","Reason":"驾驶中型以上载客载货汽车、危险物品运输车辆以外的其他机动车行驶超过规定时速10%未达20%的","count":"150","status":"0","department":"河源市公安局交通警察支队高速公路二大队","Degree":"3","Code":"1352A","Archive":"4416307900262638","Telephone":null,"Excutelocation":null,"Excutedepartment":null,"Category":"","Latefine":"0","Punishmentaccording":null,"Illegalentry":null,"Locationid":4416,"LocationName":"广东河源","DataSourceID":9992,"RecordType":"实时数据","Poundage":"25","CanProcess":"1","UniqueCode":"fbbe4d88558cff1f25a8770a42cd4e3e","SecondaryUniqueCode":"1993287","DegreePoundage":"0","Other":"","CanprocessMsg":"","CooperPoundge":"25","ActivePoundge":"-1","CanUsePackage":"0"],"ResultType":"实时数据","LastSearchTime":"","PackageName":"","RemainCount":0,"Other":""
';
$data = json_decode($jsonData,TRUE);
var_dump($data);
这个$data 是一个数组,输出数组就简单了(foreach)追问

提示 NULL 什么意思哦 ?

追答

你确定?我就能打印出来
<?php
$jsonData = '"ErrorCode":0,"Success":true,"ErrMessage":"","HasData":true,"Records":["Time":"2014-08-19 07:47:10","Location":"广东省河源市G25黄田至义合高速路段","Reason":"驾驶中型以上载客载货汽车、危险物品运输车辆以外的其他机动车行驶超过规定时速10%未达20%的","count":"150","status":"0","department":"河源市公安局交通警察支队高速公路二大队","Degree":"3","Code":"1352A","Archive":"4416307900262638","Telephone":null,"Excutelocation":null,"Excutedepartment":null,"Category":"","Latefine":"0","Punishmentaccording":null,"Illegalentry":null,"Locationid":4416,"LocationName":"广东河源","DataSourceID":9992,"RecordType":"实时数据","Poundage":"25","CanProcess":"1","UniqueCode":"fbbe4d88558cff1f25a8770a42cd4e3e","SecondaryUniqueCode":"1993287","DegreePoundage":"0","Other":"","CanprocessMsg":"","CooperPoundge":"25","ActivePoundge":"-1","CanUsePackage":"0"],"ResultType":"实时数据","LastSearchTime":"","PackageName":"","RemainCount":0,"Other":""';
$data = json_decode($jsonData,TRUE);
var_dump($data);

追问

我在试试,你的扣扣什么哦?

追答

这个没必要加qq了,上面的代码我试过的,正常运行的

参考技术A boolean tag = true; 参考技术B 楼主研究一下json_decode json_encode追问

有没有案例参考下!

写代码中遇到的问题(php接收不到传过来的json数据,php使用utf8的用法)

    今天写代码用python处理完数据,传给php服务器后,发现接收不到数据,因为是跨域,所以加了消息头:header(‘Access-Control-Allow-Origin:*‘);但是并没有什么卵用,后来不停地百度,东拼西凑终于找到了解决方案。

方案如下:

    1。传过来的数据要加header头:headers = {‘Content-Type‘: ‘application/json‘}

    2。PHP接收数据时要用:$GLOBALS[‘HTTP_RAW_POST_DATA‘]。

      原因如下:  $_POST:通过 HTTP POST 方法传递的变量组成的数组。是自动全局变量。  PHP默认只识别application/x-www.form-urlencoded标准的数据类型,也就是可以正常接受的post数据。

            如text/xml 或者 soap 或者 application/octet-stream 之类的内容无法解析,如果用$_POST数组来接收就会失败!故保留原型,交给$GLOBALS[‘HTTP_RAW_POST_DATA‘] 来接收。

 

php接收到数据以后,上传mysql后发现中文乱码,在PHP中加入header("Content-type: text/html; charset=utf-8");mysql中设置字符集utf-8也无效,后来发现是pdo没有设置中文编码,加上以后,遂成功。

另附 

  utf-8的使用方法

 “UTF-8”是标准写法,php在Windows下边英文不区分大小写,所以也可以写成“utf-8”。“UTF-8”也可以把中间的“-”省略,写成“UTF8”。一般程序都能识别,但也有例外(如下文),为了严格一点,最好用标准的大写“UTF-8”。

数据库中只能使用“utf8”(MySQL) 在mysql的命令模式中只能使用“utf8”,不能使用“utf-8”,也就是说在PHP程序中只能使用“set names utf8(不加小横杠)”,如果你加了“-”此行命令将不会生效,但是在php中header时却要加上“-”,因为IE不认识没杠的“utf8”,原因见下文。

 

PHP中的header:

 <?php header(‘Content-Type: text/html; charset=UTF-8‘); ?> //奇怪了:Content-Tyep用冒号,Chatset却是等号。
静态文件使用:
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
总结:【只有在MySQL中可以使用“utf-8”的别名“utf8”,但是在其他地方一律使用大写“UTF-8”。】

 

 





以上是关于PHP 怎么解析远程URL 传过来json ,在前端显示出来的主要内容,如果未能解决你的问题,请参考以下文章

jquery解析php通过ajax传过来的json二维数组对象

php 解析java传过来的json数据

java后台怎么接收并解析前台传过来的json

java后台怎么接收并解析前台传过来的json-CSDN论坛

前台js如何获取后台传过来的list数据?

接收到服务器传过来的json数据 怎么转成javascript对象