0

I have a problem I am getting a json file I can echo it and it looks like this

[{"sys_data":"0MxPPaza","date":"2015-02-15","objective":"VIDEO"}]

in my code I am doing this

$json = FROM THE SERVER;
$obj = json_decode($json);
$res = $obj->["objective"];
echo $res;

res is NULL obj is NULL also

5
  • try echo json_last_error_msg(). What do you get? Commented May 27, 2015 at 16:57
  • Json string is valid. But I'm not sure about $res = $obj->["objective"]; construction. Commented May 27, 2015 at 16:58
  • if $obj is null, then the json string is not valid at all. Try to display $json. make sure it is really valid. Commented May 27, 2015 at 17:01
  • @Leggendario json_last_error_msg() returns No errors SergeyChizhik if I echo only obj it is also null Commented May 27, 2015 at 17:01
  • Client has done something on their server and the json is not working I still do not understand why there were no errors in the json_last_error_msg() if json was not good. Commented May 30, 2015 at 14:12

2 Answers 2

2

Your json_decode call returns an array, with one member.

Here is a dump of your json object:

array (size=1) 0 => object(stdClass)[10] public 'sys_data' => string '0MxPPaza' (length=8) public 'date' => string '2015-02-15' (length=10) public 'objective' => string 'VIDEO' (length=5)

so replace this line:

$res = $obj->["objective"];

With this:

$res = $obj[0]->objective;
Sign up to request clarification or add additional context in comments.

Comments

0

Just replace [] quotes to {}. Like $res = $obj[0]->{"objective"};

Or you may use assoc array convertation instead of object:

$json = FROM THE SERVER;
$obj = json_decode($json, true);
$res = $obj["objective"];
echo $res;`

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.