无法在php中解析此JSON

时间:2012-08-11 19:50:11

标签: php json yql

我正在尝试使用OAuth从雅虎查询语言中删除一些邮件,一切都很好,好吧,除了,YQL返回JSON,不知怎的,我无法解析它!

我能够解析像

这样的简单JSON
'{"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!"
                }
            ]
        }
    }
}
}

我尝试过验证它     http://jsonlint.com/

它有效!

编辑:我需要在结构表中显示'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

1 个答案:

答案 0 :(得分:0)

请参阅此网址: -

Parsing JSON file with PHP

另见此网址

http://collegewires.com/parsing-json-with-php/

或尝试

对于多维数组上的迭代器,您可以使用RecursiveArrayIterator

$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";
    }
}