0

I'm attempting my first parsing of JSON with PHP and having some issues. I've searched around and even followed some solutions from SO however no luck.

So my PHP looks like:

$string = file_get_contents("SOME_URL_HERE");
$json_a = json_decode($string);

foreach($json_a->items as $mydata)

    {
         echo $mydata->uploader . "\n"; 
    }  

And that JSON url shows data that looks like (I've cut this down slightly):

{
 'apiVersion': '2.1',
 'data': {
   'updated': '2013-03-21T16:27:35.191Z',
   'totalItems': 18620,
   'startIndex': 1,
   'itemsPerPage': 5,
   'items': [
     {
       'id': 'DiiDwOJqwQg',
       'uploaded': '2012-12-22T15:20:51.000Z',
       'updated': '2013-03-18T14:44:08.000Z',
       'uploader': 'paramountmoviesuk',

Any ideas what I'm doing wrong?

Thanks

1
  • Parsing is right with all chance, your problem is with data transversing - see Crisp answer here below. Commented Mar 21, 2013 at 17:35

2 Answers 2

3

Looks like items are in data, so you probably need to do this

foreach($json_a->data->items as $mydata)
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome thats it! I see what how to handle/traverse now - cheers!
0

try json_decode( $string, true); then you can easily reach with $mydata['uploader']

EDIT : you can loop it through with this statement if you don't want to use object mapping;

foreach( json_a['data']['items'] as $my_data)
{
    echo $my_data['uploader'] . "\n";
}

if it's not working, please make sure that it is a valid json in this website

1 Comment

Tried that from what i read on php.net tutorials. Didn't work

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.