php json_decode 在 webservice 调用后返回 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/689185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:30:45  来源:igfitidea点击:

json_decode returns NULL after webservice call

phpjson

提问by schouk

There is a strange behaviour with json_encodeand json_decodeand I can't find a solution:

有一个奇怪的行为json_encodejson_decode我不能找到一个解决方案:

My php application calls a php web service. The webservice returns json that looks like this:

我的 php 应用程序调用了一个 php web 服务。Web 服务返回如下所示的 json:

var_dump($foo):
string(62) "{"action":"set","user":"123123123123","status":"OK"}"

now I like to decode the json in my application:

现在我喜欢在我的应用程序中解码 json:

$data = json_decode($foo, true)

but it returns NULL:

但它返回NULL

var_dump($data):
NULL

I use php5. The Content-Type of the response from the webservice: "text/html; charset=utf-8"(also tried to use "application/json; charset=utf-8")

我使用php5。来自网络服务的响应的内容类型:("text/html; charset=utf-8"也尝试使用"application/json; charset=utf-8"

What could be the reason?

可能是什么原因?

回答by Pablo

Well, i had a similar issue and the problems was the PHP magic quotes in the server... here is my solution:

好吧,我有一个类似的问题,问题是服务器中的 PHP 魔术引号……这是我的解决方案:

if(get_magic_quotes_gpc()){
  $param = stripslashes($_POST['param']);
}else{
  $param = $_POST['param'];
}
$param = json_decode($param,true);

回答by Stefan Gehrig

EDIT:Just did some quick inspection of the string provided by the OP. The small "character" in front of the curly brace is a UTF-8 B(yte) O(rder) M(ark)0xEF 0xBB 0xBF. I don't know why this byte sequence is displayed as ?here.

编辑:刚刚对 OP 提供的字符串进行了一些快速检查。花括号前面的小“字符”是UTF-8 B(yte) O(rder) M(ark)0xEF 0xBB 0xBF。我不知道为什么这个字节序列显示在?这里。

Essentially the system you aquire the data from sends it encoded in UTF-8 with a BOM preceding the data. You should remove the first three bytes from the string before you throw it into json_decode()(a substr($string, 3)will do).

本质上,您从中获取数据的系统发送它以 UTF-8 编码,并在数据之前带有 BOM。在将字符串放入json_decode()(asubstr($string, 3)会做)之前,您应该从字符串中删除前三个字节。

string(62) "?{"action":"set","user":"123123123123","status":"OK"}"
            ^
            |
            This is the UTF-8 BOM

As Kuroki Kazediscovered, this character surely is the reason why json_decodefails. The string in its given form is not correctly a JSON formated structure (see RFC 4627)

正如黑木风发现的那样,这个角色肯定是json_decode失败的原因。其给定形式的字符串不是正确的 JSON 格式结构(请参阅RFC 4627

回答by LarryBattle

Print the last json error when debugging.

调试时打印最后一个json错误。

json_decode( $so, true, 9 );
$json_errors = array(
    JSON_ERROR_NONE => 'No error has occurred',
    JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
    JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
    JSON_ERROR_SYNTAX => 'Syntax error',
);
 echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
还可以使用 json.stringify() 函数仔细检查您的 JSON 语法。

回答by djb

None of the solutions above worked for me, but html_entity_decode($json_string)did the trick

上面的解决方案都不适合我,但html_entity_decode($json_string)成功了

回答by ólafur Waage

Try this

尝试这个

$foo = utf8_encode($foo);
$data = json_decode($foo, true);

回答by ikary

make sure that if you sent the data by POST / GET, the server has not escape the quotes

确保如果您通过 POST / GET 发送数据,则服务器没有转义引号

$my_array = json_decode(str_replace ('\"','"', $json_string), true);

回答by Kuroki Kaze

"?{"action":"set","user":"123123123123","status":"OK"}"

This little apostrophe in the beginning - what is it? First symbol after the doublequote.

开头的这个小撇号 - 是什么?双引号后的第一个符号。

回答by mona

I had the similar problem in a live site. In my local site it was working fine. For fixing the same I Just have added the below code

我在一个实时站点中遇到了类似的问题。在我的本地站点,它运行良好。为了修复相同的问题,我刚刚添加了以下代码

json_decode(stripslashes($_GET['arr']));

json_decode(stripslashes($_GET['arr']));

回答by Ivan Ivanovych Lukyanchuk

I just put this

我只是把这个

$result = mb_convert_encoding($result,'UTF-8','UTF-8'); 
    $result = json_decode($result);

and it's working

它正在工作

回答by Matija

Yesterday I spent 2 hours on checking and fixing that error finally I found that in JSON string that I wanted to decode were '\' slashes. So the logical thing to do is to use stripslashes function or something similiar to different PL.

昨天我花了 2 个小时检查并修复该错误,最后我发现在 JSON 字符串中我想解码的是 '\' 斜杠。所以合乎逻辑的做法是使用stripslashes函数或类似不同PL的东西。

Of course the best way is sill to print this var out and see what it becomes after json_decode, if it is null you can also use json_last_error()function to determine the error it will return integer but here are those int described:

当然,最好的方法是打印这个 var 并查看它在 json_decode 之后变成什么,如果它是 null,你也可以使用json_last_error()函数来确定它将返回整数的错误,但这里是那些 int 描述:

0 = JSON_ERROR_NONE

0 = JSON_ERROR_NONE

1 = JSON_ERROR_DEPTH

1 = JSON_ERROR_DEPTH

2 = JSON_ERROR_STATE_MISMATCH

2 = JSON_ERROR_STATE_MISMATCH

3 = JSON_ERROR_CTRL_CHAR

3 = JSON_ERROR_CTRL_CHAR

4 = JSON_ERROR_SYNTAX

4 = JSON_ERROR_SYNTAX

5 = JSON_ERROR_UTF8

5 = JSON_ERROR_UTF8

In my case I got output of json_last_error() as number 4so it is JSON_ERROR_SYNTAX. Then I went and take a look into the string it self which I wanted to convert and it had in last line:

在我的例子中,我得到的 json_last_error() 输出为4,所以它是JSON_ERROR_SYNTAX。然后我去看看我想转换的字符串它自己,它在最后一行:

'\'title\' error ...'

After that is really just an easy fix.

在那之后真的只是一个简单的修复。

$json = json_decode(stripslashes($response));
if (json_last_error() == 0) { // you've got an object in $json}