1

I have a JSON Object returned from the remote Web Service (via Curl call). The Object is something like this:

stdClass Object ( [https://example.com] => stdClass Object ( [hash] => 8 [id] => 277 ) )

How am i suppose to access the values like: hash, id from this Object please?

I tried:

$Object = json_decode( $curl_return );

echo $Object->hash; // Didn't work!
echo $Object[0]->hash; // Didn't work!
echo $Object[0]['hash']; // Didn't work!
echo $Object['https://example.com']->hash; // Didn't work!

Please kindly help.

夏期劇場
  • 17,821
  • 44
  • 135
  • 217
  • Also make sure not to overlook: http://stackoverflow.com/q/33157296/3933332 since you got an invalid property name here. – Rizier123 Sep 01 '16 at 11:20

2 Answers2

2

This will work:

$url = 'https://example.com';

echo $Object->$url->hash;

Alternatively you can decode the JSON to an associative array instead of a \stdClass by setting the second argument to true with:

json_decode($json, true);

https://secure.php.net/manual/en/function.json-decode.php

siannone
  • 6,617
  • 15
  • 59
  • 89
1

Pass TRUE as the second argument to json_encode() and it returns arrays, not objects.

All you have to do then is to access the values using the usual array access syntax, with square brackets:

echo($Object['https://example.com']['hash']);
axiac
  • 68,258
  • 9
  • 99
  • 134