Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse json with php 5 not working

Tags:

json

php

I am trying to parse and list a json file. I have it as Unicode-8 without BOM. File is operational. Structure:

// "games.json" :
// {"data":[
// {"game":"5359","Date":"07/08/2015"},
// {"game":"5355","Date":"10/20/2007"},
....


<?php

// copy file content into a string var
$jsondata = file_get_contents("games.json");

// convert the string to a json object
$json = json_decode($jsondata,true);

var_dump($json); // DW!

foreach($json["data"] as $data_X)

{echo $data_X->game;} 
?>

Why doesn't this work?

like image 212
verlager Avatar asked Oct 20 '22 05:10

verlager


2 Answers

<?php
$jsonData = '{ "user":"John", "age":22, "country":"United States" }';
$phpArray = json_decode($jsonData);
print_r($phpArray);
foreach ($phpArray as $key => $value) { 
echo "<p>$key | $value</p>";
}

?>

like image 161
Syam Danda Avatar answered Oct 21 '22 23:10

Syam Danda


use $json = json_decode($jsondata); instead of `$json = json_decode($jsondata,true); True parameter convert it into array not json object.

like image 28
Jasbir Singh Sohanpal Avatar answered Oct 22 '22 00:10

Jasbir Singh Sohanpal