The received json cannot be parsed and must be parsed using php.

Source: Internet
Author: User
The received json cannot be parsed and must be parsed using php.
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 // timeout (unit: s); $ context = stream_context_create ($ options); $ result = file_get_contents ($ url, false, $ context); return $ result;} $ url = "http://chepiao.sinaapp.c Om/api. php? Act = remain "; $ data = array (" date "=>" 20131220 "," startStation "=>" Beijing "," arriveStation "=>" Tianjin "); $ result = sendPost ($ url, $ data); $ obj = json_decode ($ result); $ result indicates that the returned data cannot be parsed, and json_decode ($ result) is used ); parsed to NULL


Reply to discussion (solution)

Help resolve

Print $ result to view the result

Really good.
See in the gbk environment
{"Title ":"Beijing-Tianjin?? 0 items splash? "," ErrMsg ":"???? Why? Why ?? Parameters ???? Xuan ?? "}
UTF-8 environment
{"Title ":"????-??

This post was last edited by xuzuning at 19:19:36, January 17 ,.

Even csdn cannot accept this character set disorder
UTF-8 environment

Amazing
Echo json_last_error ();
The result is 4, indicating that the format is incorrect.
So I threw the returned content at http://jsonlint.com/detection, and reported an error, which is a line change problem.
Print_r (json_decode (str_replace ("\ n", "", $ result )));
The result is fine.

Thank you for not using json_last_error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.