0

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
  • 3
    Define "not able to parse it". What code do you use? What result do you get? What errors are reported? – Quentin Aug 11 '12 at 19:53
  • 1
    What errors do you get (if any)? I just copied that and called `json_decode` and it successfully returned a `stdClass` object to me with all the correct data. – drew010 Aug 11 '12 at 19:53
  • If you are using `json_decode()`, try running `json_last_error()` to see what's wrong. http://www.php.net/manual/en/function.json-last-error.php – A Person Aug 11 '12 at 19:53
  • Works for me: http://codepad.org/ehd6Q9QB – gen_Eric Aug 11 '12 at 19:53
  • have you looked if there are white spaces or non breaking spaces that could be trimmed off the string or whatever which would make the string bugged when they're there? – Hugo Dozois Aug 11 '12 at 19:54
  • 2
    Sushas, if you use json_decode($json_data,true), you can get the data in PHP array form. – Buttle Butkus Aug 11 '12 at 19:59
  • What result do you get if you remove the line `$json_data = json_encode($sdata);` and change `$json_data` on the next line to `$sdata`? – uınbɐɥs Aug 11 '12 at 20:10
  • when i remove that line - and try doing $mails->{'query'} Catchable fatal error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\yahoo\txtweb\yql.php on line 21 –  Aug 11 '12 at 20:17
  • What exactly is `$sdata`? Give an example. – salathe Aug 11 '12 at 20:27

1 Answers1

0

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:\n";
    } else {
        echo "$key => $val\n";
    }
}
Community
  • 1
  • 1
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • i have add improve my answer please take a look. – Abid Hussain Aug 11 '12 at 19:59
  • Gives me this: Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in C:\xampp\htdocs\yahoo\txtweb\yql.php:22 Stack trace: #0 C:\xampp\htdocs\yahoo\txtweb\yql.php(22): ArrayIterator->__construct('{"query":{"coun...') #1 {main} thrown in C:\xampp\htdocs\yahoo\txtweb\yql.php on line 22 –  Aug 11 '12 at 20:02
  • Just remove the double-encoding - see @Kaii's answer. – uınbɐɥs Aug 11 '12 at 20:03