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?

1
  • 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 jsonlint.com . Commented Mar 26, 2017 at 4:57

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

No problem - if this worked then don't forget to give this answer a green tick ;-)
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.

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.