Can';不要在php中解析这个JSON
我正在尝试使用OAuth从Yahoo查询语言中获得一些邮件,一切都很好,除了YQL返回JSON,不知何故我无法解析它 我能够解析简单的JSON,比如Can';不要在php中解析这个JSON,php,json,yql,Php,Json,Yql,我正在尝试使用OAuth从Yahoo查询语言中获得一些邮件,一切都很好,除了YQL返回JSON,不知何故我无法解析它 我能够解析简单的JSON,比如 '{"hello":"world"}' 但不是这个: { "query": { "count": 1, "created": "2012-08-11T19:22:51Z", "lang": "en-US", "results": { "result": { "messageI
'{"hello":"world"}'
但不是这个:
{
"query": {
"count": 1,
"created": "2012-08-11T19:22:51Z",
"lang": "en-US",
"results": {
"result": {
"messageInfo": [
{
"from": {
"name": "account-services-us@cc.yahoo-inc.com"
},
"subject": "Success! You have shared your Yahoo! information"
},
{
"from": {
"name": "account-services-in@cc.yahoo-inc.com"
},
"subject": "Success! You have shared your Yahoo! information."
},
{
"from": {
"name": "account-services-in@cc.yahoo-inc.com"
},
"subject": "Success! You have shared your Yahoo! information."
},
{
"from": {
"name": "Yahoo!"
},
"subject": "Welcome to Yahoo!"
}
]
}
}
}
}
我试着用计算机验证它
而且它是有效的
编辑:我需要在类似表格的结构中显示“from:name”和“subject”
我编写的代码片段如下:
$sdata = call_yql(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
$access_token, $access_token_secret,
false, true);
$json_data = json_encode($sdata);
$mails = json_decode($json_data);
print_r($mails->query);
我得到的错误是:
Notice: Trying to get property of non-object in C:\xampp\htdocs\yahoo\txtweb\yql.php on line 21
请参阅以下网址:-
请参见此url
或者试试看
要对多维数组进行迭代,可以使用
定义“无法解析它”。你用什么代码?你得到了什么结果?报告了哪些错误?您得到了哪些错误(如果有)?我刚刚复制了它并调用了
json\u decode
,它成功地向我返回了一个包含所有正确数据的stdClass
对象。如果您使用的是json\u decode()
,请尝试运行json\u last\u error()
,看看有什么问题。适用于我:Sushas,如果您使用json_decode($json_data,true),您可以以PHP数组形式获取数据。我添加了改进我的答案,请看一看。告诉我:致命错误:未捕获异常“InvalidArgumentException”,带有消息“传递的变量不是数组或对象,在C:\xampp\htdocs\yahoo\txtweb\yql.php:22堆栈跟踪中改为使用空数组:#0 C:\xampp\htdocs\yahoo\txtweb\yql.php(22):ArrayIterator->u构造(“{”query:“{”conu…)#1{main}在C:\xampp\htdocs\yahoo\txtweb\yql.php第22行抛出只要删除双重编码即可-请参阅@Kaii的答案。
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:\n";
} else {
echo "$key => $val\n";
}
}