0

I am trying to access data from json in PHP but it seems not working.

code:

$raw =file_get_contents("http://api.mydomain.com/data.json");
$data = json_decode($raw->list);
echo $data;

I'm getting error that list is not an object.

Here is my json

{ "list" : [ { "data1":" my data"}, {"data2": "my data 2"}]};

What did u do wrong? Also how can i access data1 and others?

sse
  • 47
  • 1
  • 10
  • Have you tried running your JSON through a validator? The semi-colon isn't valid which a validator confirms. Try it for yourself by pasting it into JSONLint at http://jsonlint.com/ . – Louis Langholtz Mar 26 '17 at 04:57

1 Answers1

1

You don't need the $raw->list bit and you are getting an object back from json_decode so use print_r and not echo

$raw = file_get_contents("http://api.mydomain.com/data.json");
$data = json_decode($raw);
print_r($data);
Chris
  • 4,672
  • 13
  • 52
  • 93
  • No problem - if this worked then don't forget to give this answer a green tick ;-) – Chris Mar 25 '17 at 16:31
  • By the way, if you're less familiar with PHP objects then you can use `$data = json_decode($raw,true);` and you'll get a PHP array instead. – Chris Mar 25 '17 at 16:39