The received json cannot be parsed. it must be parsed using php. how can this problem be solved?

Source: Internet
Author: User
The received json file cannot be parsed. you must use php to parse function & nbsp; sendPost ($ url, $ postData) {& nbsp; $ postdata & nbsp; http_build_query ($ postData); & nbsp; $ options & nbsp; array (& nbsp; http & nbsp; & gt; & the received json cannot be parsed, 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.com/api.php? Act = remain ";
$ Data = array (
"Date" = & gt; "20131220 ",
"StartStation" => "Beijing ",
"ArriveStation" => "Tianjin"
);
$ Result = sendPost ($ url, $ data );
$ Obj = json_decode ($ result );
$ Result is the returned data, that is, the data cannot be parsed, and json_decode ($ result) is used; the parsed result is NULL.

Share: More


------ Solution --------------------

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

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

------ Solution --------------------
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.

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.