Home >Backend Development >PHP Tutorial >接收到的json解析不了,必须用php解析

接收到的json解析不了,必须用php解析

WBOY
WBOYOriginal
2016-06-23 14:39:43836browse

function sendPost($url,$postData){ $postdata = http_build_query($postData); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超时时间(单位:s)) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; }$url = "http://chepiao.sinaapp.com/api.php?act=remain";$data = array( "date"=>"20131220", "startStation"=>"北京", "arriveStation"=>"天津");$result = sendPost($url, $data); $obj = json_decode($result);$result就是返回来的数据,就是解析不了,用json_decode($result);解析成的是NULL


回复讨论(解决方案)

来人帮解析一下 

打印下$result看下结果

真够可以的
gbk环境看到
{"title":"北京 - 天津 ??0娆¤溅锛?","errMsg":"????ㄥ?锛??绋????璇??"}
utf-8环境看到
{"title":"???? - ??

本帖最后由 xuzuning 于 2013-12-17 19:19:36 编辑

连csdn都不能接受这种字符集的错乱
utf-8环境看到

真奇葩
echo json_last_error();
结果是4,说明格式不正确
于是把返回的东西扔http://jsonlint.com/检测,报错,发现是换行的问题
print_r(json_decode(str_replace("\n"," ",$result)));
结果就好了。

多谢各位 json_last_error没学会用,结贴了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn