Json_decode cannot parse the returned data

Source: Internet
Author: User
Tags php editor
Json_decode cannot parse the returned data on the server:
function order_kb(){    $orderaddress[0]['odd']='560860393435';    $this->ajaxReturn($orderaddress,'JSON');}


Client:
 $v){    $o.="$k=".urlencode($v)."&";}$data=substr($o,0,-1);$ch = curl_init();curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER,0);curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);$result = curl_exec($ch);curl_close($curl);print_r('Client:'.$result.'|');print_r('Decode:'.json_decode($result,true).'|');?>

The obtained data [{"odd": "560860393435"}] cannot be parsed.


Reply to discussion (solution)

When you print the string and the array, will not an error be reported ??

Server:

function order_kb(){    $orderaddress[0]['odd']='560860393435';    $this->ajaxReturn($orderaddress,'JSON');}


Client:
 $v){    $o.="$k=".urlencode($v)."&";}$data=substr($o,0,-1);$ch = curl_init();curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER,0);curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);$result = curl_exec($ch);curl_close($curl);print_r('Client:'.$result.'|');print_r('Decode:'.json_decode($result,true).'|');?>

The obtained data [{"odd": "560860393435"}] cannot be parsed.

No

Print_r ('Client: '. $ result.' | ');
Client: commandid {"odd": "560860393435"}] |
Apparently, a BOM header exists.
So we need
Print_r (json_decode (substr ($ result, 3), true ));
Get
Array
(
[0] => Array
(
[Odd] = & gt; 560860393435
)

)

When you print the string and the array, will not an error be reported ??

No. the same result is returned when the string is removed.

Print_r ('Client: '. $ result.' | ');
Client: commandid {"odd": "560860393435"}] |
Apparently, a BOM header exists.
So we need
Print_r (json_decode (substr ($ result, 3), true ));
Get
Array
(
[0] => Array
(
[Odd] = & gt; 560860393435
)

)

Can I filter through the order_kb () function on the server?

You save the program fileUTF-8 without bomFormat

You save the program fileUTF-8 without bomFormat

Files with notepad looked at, are UTF-8 format

If you are using Notepad to edit the php file, select ANSI when saving the file.
Generally, there is no Chinese character string constant in the program, so if UTF-8 is not selected, it will not affect the whole process.

Someone may tell you to add ob_clean ();
However, this anti-human approach is really undesirable.

If you are using Notepad to edit the php file, select ANSI when saving the file.
Generally, there is no Chinese character string constant in the program, so if UTF-8 is not selected, it will not affect the whole process.

Someone may tell you to add ob_clean ();
However, this anti-human approach is really undesirable.

I used Dreamware to edit the php file. I used notepad to check that the file encoding was UTF-8. Whether php's ajaxReturn is added with the BOM header by default. how can I prevent it from automatically adding

The BOM header is added to your editor (because the default operating system character set is GBK) to indicate the character set of the document

The BOM header is added to your editor (because the default operating system character set is GBK) to indicate the character set of the document

Except for ajaxReturn, is there any other way to return the value back? I think someone else can use json_decode directly to retrieve the interface value. you don't need to process the BOM header.

People's program files do not have BOM headers, so they naturally do not need to be processed!
If you want to use the basic editor to edit php files and stick to UTF-8 encoding, you need to handle the BOM header yourself.

People's program files do not have BOM headers, so they naturally do not need to be processed!
If you want to use the basic editor to edit php files and stick to UTF-8 encoding, you need to handle the BOM header yourself.

Should I use the php editor to create a new file and paste the content to it?

People's program files do not have BOM headers, so they naturally do not need to be processed!
If you want to use the basic editor to edit php files and stick to UTF-8 encoding, you need to handle the BOM header yourself.


This does not work if there is no BOM header.

People's program files do not have BOM headers, so they naturally do not need to be processed!
If you want to use the basic editor to edit php files and stick to UTF-8 encoding, you need to handle the BOM header yourself.


Add ob_end_clean ();

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.