1

Yes, I have read previous questions and I know how to access a JSON object and how to convert it into an array. I know about json_encode/decode. My problem is that my JSON response has a string, arrays and all and it will always return NULL when I access the data directly.

  object(Unirest\Response)#8 (4) {
    ["code"]=>
      int(200)
    ["body"]=>
      string(666) "{ "ticker": "AAPL:US", ".."
    ["headers"]=>
    array(9) {
    [0]=>
    string(15) "HTTP/1.1 200 OK"

Normally you would be able to directly access the object like this and this worked just fine when I last accessed the script a few days ago:

$response->body->ticker

Or you could use json_decode with true to turn it into an array.

$array = json_decode($response->body, true);

However, all of this no longer works. I believe they changed something with the output because it was working just a while ago but I have no clue. Any idea how to access the ticker data? I tested it with a different API and the same commands are working just fine to retrieve data from a different API, but the output seems to be different.

mashup
  • 237
  • 2
  • 12
  • What is the output of [json_last_error](http://php.net/manual/en/function.json-last-error.php)? What is the complete string being parsed? – MirroredFate Jul 24 '15 at 18:40
  • 1
    that's not json. that's a var_dump of a PHP object, which contains somewher a json **STRING**. you cannot access that string's components as an object until AFTER it's been decoded. – Marc B Jul 24 '15 at 19:03

2 Answers2

0

$response->body is a json string assuming you didnt shorten it so much as to loose someting important and therefore needs to be seperately converted to a PHP data item.

As its a json string representing an object why not convert it to a PHP object like so

$body = json_decode($response->body);

Then you can address its properties like

$body->ticker

Alternatively

$response->body = json_decode($response->body);

Now you can address it as you expected i.e.

$response->body->ticker
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thank you for the answer, but I already tried that. First thing I tried. I am getting Trying to get property of non-object when I use it like that. – mashup Jul 24 '15 at 19:40
  • There seems to be something wrong with the JSON formatting, else this would work, but doesnt seem like I will find it, tried it for hours, every variation – mashup Jul 24 '15 at 19:41
  • In that case post a complete json string and not just a piece of it. – RiggsFolly Jul 24 '15 at 21:31
0

Ok, finally solved it after reading this answer: PHP json_decode() returns NULL with valid JSON?

Apparently, as I assumed earlier it was a formatting issue. I did not know that JSON would return NULL if the object includes non-UTF8 code and/or BOM codes.

I couldn't find any BOM codes but I suppose there was some non-UTF8 that was breaking it.

Long story short this works:

$dec =  json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $response->body), true );         
 echo $dec['ticker'];

Well at least I now know a lot more about JSON which will come in handy some day ;)

Community
  • 1
  • 1
mashup
  • 237
  • 2
  • 12