localhost
localhost

Reputation: 1082

Parse a JSON feed

I'm having trouble retrieving "name","att","cmp", and "yds" in this NFL Game Center JSON feed. Using PHP.

How can I retrieve these values without hard-coding "2015091700" and "00-0023436"?

http://www.nfl.com/liveupdate/game-center/2015091700/2015091700_gtd.json

{  
"2015091700":{  
  "home":{  
     "stats":{  
        "passing":{  
           "00-0023436":{  
              "name":"A.Smith",
              "att":25,
              "cmp":16,
              "yds":191
           }
        }
     }
  }
 }
}

This is the code I have right now.

$url  = file_get_contents("http://www.nfl.com/liveupdate/game-center/2015091700/2015091700_gtd.json");
$json = json_decode($url, true); // 'true' makes data an array
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json)); 
$player = array();

foreach($iterator as $key=>$value) {
  $player[$key] = $value;
  echo $player['name'];
}

This is close to what I need. However, the page spits out:

A.Smith 
A.Smith
A.Smith
A.Smith
A.Smith
A.Smith
A.Smith
A.Smith
J.Charles
J.Charles
J.Charles
J.Charles
J.Charles
J.Charles
J.Charles
J.Charles
A.Smith
A.Smith
A.Smith
A.Smith
A.Smith

I'm not sure why I'm getting duplicate values. How do I make this reflect what the JSON feed has? I'd ideally like to create a query string that I can insert into my MySQL database table.

Upvotes: 3

Views: 163

Answers (3)

Jay Blanchard
Jay Blanchard

Reputation: 34416

One way to do this is by using PHP's RecursiveIteratorIterator class which allows you to iterate through any of the other iterator classes and the RecursiveArrayIterator class which allows easy iteration through an array. These are part of PHP's SPL Library. You can use it like this:

<?php
$data = '{
"2015091700":{  
  "home":{  
     "stats":{  
        "passing":{  
           "00-0023436":{  
              "name":"A.Smith",
              "att":25,
              "cmp":16,
              "yds":191
           }
        }
     }
  }
 }
}';

$json = json_decode($data, true); // 'true' makes data an array

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json)); 

foreach($iterator as $key=>$value) {
    echo $key .' '. $value . '<br />';
}

?>

The result is:

name A.Smith
att 25
cmp 16
yds 191

EXAMPLE

If you want to get individual bits into variables the easiest way it to make a small change to the code:

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json)); 

$player = array();

foreach($iterator as $key=>$value) {
    $player[$key] = $value;

}
echo $player['name'];

This allows you to describe a player and assign the keys properly to the player. The result of the above would be:

A.Smith

Now you can use the various parts of the player's data as you need as they are readily identified by their keys.

Upvotes: 4

gen_Eric
gen_Eric

Reputation: 227240

Decode the JSON as an associative array, then loop over it with foreach.

$data = json_decode($json, true);
foreach($data as $key=>$val){
    // $key = 2015091700
    // $val = an associative array with one row.
    // ['home' => ... ]
}

Upvotes: 1

Reza S
Reza S

Reputation: 9748

Does this help?

$decoded = json_decode($json,true);
var_dump($decoded['2015091700']);

Upvotes: 0

Related Questions