duanouyong4228 2012-08-11 19:50
浏览 74
已采纳

无法在php中解析此JSON

I'm trying to get off some mails from Yahoo Query Language using OAuth, everything is fine and alright, except, YQL returns JSON, and somehow I'm not able to parse it!

I'm able to parse simple JSONs like

'{"hello":"world"}'

but not this one:

{
"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!"
                }
            ]
        }
    }
}
}

I tried validating it in http://jsonlint.com/

and it's valid!

Edit: I need to display 'from:name' and 'subject' in a table like structure.

The code snippet I wrote is this:

$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);

The Error i get is:

Notice: Trying to get property of non-object in C:\xampp\htdocs\yahoo\txtweb\yql.php       on line 21
  • 写回答

1条回答 默认 最新

  • doubianyu7844 2012-08-11 19:55
    关注

    See this url:-

    Parsing JSON file with PHP

    see also this url

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

    or try it

    To iterator over a multidimensional array, you can use the RecursiveArrayIterator

    $jsonIterator = new RecursiveIteratorIterator(
        new RecursiveArrayIterator(json_decode($json, TRUE)),
        RecursiveIteratorIterator::SELF_FIRST);
    
    foreach ($jsonIterator as $key => $val) {
        if(is_array($val)) {
            echo "$key:
    ";
        } else {
            echo "$key => $val
    ";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程