0

I have a problem with json parsing. I have already read the many questions here on stackoverflow but I can't figured out what I'm missing.

In my site I use Facebook Api to post my feed using curl and it respond with a json message. I take this response and I save it in my database.
In my Backoffice I need to retrieve this message and print it in case of error.

Here an example about an error message:

{"error":{"message":"(#1500) The url you supplied is invalid","type":"OAuthException","code":1500}}

In my php page I need to get just the message part so I did:

$message = get from the db and fetch;
$error_array = json_decode($message,true);
print_r($error_array);

but it doesn't print anything, just a blank page.
If I just print $message I can see the entire string.

What am I missing? This issue it's driving me crazy all day long!!

3
  • You do not have JSON functionality enabled for your PHP installation Commented Oct 30, 2013 at 13:06
  • quite possible, if he is using PHP version < 5.2 Commented Oct 30, 2013 at 13:08
  • The problem was a bad encoding of the data when I retrieve it from the DB. Commented Oct 30, 2013 at 13:16

2 Answers 2

2

I tried the following:

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

?>

and getting output:

Array
(
    [error] => Array
        (
            [message] => (#1500) The url you supplied is invalid
            [type] => OAuthException
            [code] => 1500
        )
)

It's working as intended.

I suspect the problem is with this:

$message = get from the db and fetch;

After you load the $message variable, do a var_dump($message) and see if the string is in there (as expected).

Sign up to request clarification or add additional context in comments.

3 Comments

it is the answer. your provided JSON string is perfectly valid. so the only reason your code does not work is that you get the wrong output from your DB. Possibly because of html entity encoding or something similar
if he doesn't have json_encode or json_decode functionality in his PHP, he would have gotten a function not exists error - which would have been shown on page (if display_error is on) or logged to file (if error_log is enabled).
Thanks Latheesan Kanes the problem was the encoded string, when I retrieve the data from the db I get the double quotes parsed so it caused the errors. Thanks again
0

$jsonString = '["[email protected]","[email protected]","[email protected]"]'; $arrayOfEmails=json_decode($jsonString);

Or

$jsonString = "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]"; $arrayOfEmails=json_decode($jsonString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.