我遇到了json解析的问题。 我已经在stackoverflow上阅读了很多问题,但我无法弄清楚我错过了什么。
在我的网站中,我使用Facebook Api使用curl发布我的Feed,并使用json消息进行响应。 我接受了这个回复,并将其保存在我的数据库中 在我的Backoffice中,我需要检索此消息并在出现错误时将其打印出来。
这是一个关于错误消息的示例:
{"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
,我可以看到整个字符串。
我错过了什么? 这个问题让我整天疯狂!!
答案 0 :(得分: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
变量后,执行var_dump($message)
并查看字符串是否在那里(如预期的那样)。
答案 1 :(得分: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);