0

我对 json 解析有疑问。我已经阅读了关于 stackoverflow 的许多问题,但我无法弄清楚我错过了什么。

在我的网站中,我使用 Facebook Api 使用 curl 发布我的提要,并以 json 消息响应。我接受了这个回复并将其保存在我的数据库中。
在我的后台,我需要检索此消息并在出现错误时将其打印出来。

这是一个关于错误消息的示例:

{"error":{"message":"(#1500) The url you supplied is invalid","type":"OAuthException","code":1500}}

在我的 php 页面中,我只需要获取message一部分,所以我做了:

$message = get from the db and fetch;
$error_array = json_decode($message,true);
print_r($error_array);

但它不打印任何东西,只是一个空白页。
如果我只是打印$message,我可以看到整个字符串。

我错过了什么?这个问题整天让我发疯!!

4

2 回答 2

2

我尝试了以下方法:

<pre>
<?php

$jsonStr = '{"error":{"message":"(#1500) The url you supplied is invalid","type":"OAuthException","code":1500}}';

$error_array = json_decode($jsonStr, true);

print_r($error_array);

?>

并获得输出:

Array
(
    [error] => Array
        (
            [message] => (#1500) The url you supplied is invalid
            [type] => OAuthException
            [code] => 1500
        )
)

它按预期工作。

我怀疑问题出在这个:

$message = get from the db and fetch;

加载$message变量后,执行 avar_dump($message)并查看字符串是否在其中(如预期的那样)。

于 2013-10-30T13:07:01.627 回答
0

$jsonString = '["m@gmail.com","b@gmail.com","c@gmail.com"]'; $arrayOfEmails=json_decode($jsonString);

或者

$jsonString = "[\"a@gmail.com\",\"b@gmail.com\",\"c@gmail.com\"]"; $arrayOfEmails=json_decode($jsonString);

于 2013-10-30T13:08:57.217 回答