-2

I am trying to get data from an api where the result json has no square brackets, and looks like this

{
  "status":1,
  "time":42868,
  "response":{
    "some_item":"itemval1",
    "itemstat":"itemstatvalue"
  }
}

I'm trying to get an array through json_decode, but i can't get anywhere I've been trying for hours...

$data = json_decode($result);

by doing such i get the data back in a large string, but whenever i try to access it such as

echo $data->time;

or

echo $data[0];

I get an error saying 500 where my code is invalid, so I'm wondering how I would convert this to an array where i can access these values. Whenever I just echo $data though i get a ridiculously large string full of all of the values i need, but can't access them.

Phil
  • 157,677
  • 23
  • 242
  • 245
Chris Hutchison
  • 601
  • 6
  • 20
  • That is not valid JSON. – Don't Panic Aug 14 '17 at 22:56
  • how would i make this valid json, because I'm getting data from an api and this is the format it returns it in – Chris Hutchison Aug 14 '17 at 22:56
  • Either `"itemval1","itemstat"` need to be contained in square brackets, or `$itemstat` needs to be the value of a different key. I'm afraid I don't really know what you can do about the API returning invalid JSON. – Don't Panic Aug 14 '17 at 22:58
  • i misformatted the json when i entered it is it still not possible when you look at the updated formatting – Chris Hutchison Aug 14 '17 at 22:59
  • 1
    Yes, it is valid after your edit. But now, it seems `echo $data->time;` should be working. (`echo $data[0];` will _not_ work, because `$data` is an object, and doesn't have a [0] property even if it was an array). – Don't Panic Aug 14 '17 at 23:02
  • 1
    Not sure what is the problem there... http://sandbox.onlinephpfunctions.com/code/e905724d84691403718dd62b22a43bf8f6a4147b – Dekel Aug 14 '17 at 23:02
  • 1
    I'm confused by the fact that you are able to `echo $data` and get a large string. `$data` should be an object and `echo` shouldn't really work on it. That should produce a fatal error. – Don't Panic Aug 14 '17 at 23:03
  • 1
    If you're getting a 500 error, check your HTTP server's error log file. The cause will be listed there – Phil Aug 14 '17 at 23:04
  • thats what confuses me too @dontpanic i tried vardump but got an error so i used echo and it worked just out of curiosity – Chris Hutchison Aug 14 '17 at 23:04
  • 1
    I think you're really missing a [mcve] here. Given the input and the minimal code you've shown, I get the expected result, and I assume others will too. – Don't Panic Aug 14 '17 at 23:06
  • 1
    Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – localheinz Aug 14 '17 at 23:06
  • Are you sure you're not using `json_encode` accidentally? – Phil Aug 14 '17 at 23:14
  • i think the issue is the json returned does not start and end with a single quote – Chris Hutchison Aug 14 '17 at 23:45

1 Answers1

0

try the code below. Then, you would be able to understand how json_decode works and how to access the properties.

You can see the result here

<?php
$s = '{
  "status":1,
  "time":42868,
  "response":{
    "some_item":"itemval1",
    "itemstat":"itemstatvalue"
  }
}';

$object = json_decode($s);

var_dump($object);

echo PHP_EOL . '------------------------' . PHP_EOL;

$array = json_decode($s, true);

var_dump($array);

echo PHP_EOL . '------------------------' . PHP_EOL;

echo $object->response->some_item;

echo PHP_EOL . '------------------------' . PHP_EOL;

echo $array['response']['some_item'];
Mojtaba
  • 4,852
  • 5
  • 21
  • 38