Im new to json, so i gotta ask you a maybe really simple questions. I've tried searching around but have not found anything I can get to work. I have called an API and received the data in json. And now comes my problem parsing it through my php, it "will not find anything." My code looks like this:
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/vnd.travify.v1+json\r\n"
)
);
$context = stream_context_create($opts);
$url = 'http://oda.ft.dk/api/Sag?$select=titel,Sagskategori/kategori&$expand=Sagskategori';
$output = file_get_contents($url, false, $context);
$string = file_get_contents($url, false, $context);
$result = json_decode($string, true);
$i = -1;
foreach ($result as $data) {
$i++;
echo "#";
echo $i;
echo "<br>";
echo "<b>Test 1:</b>";
echo "<br>";
if(!empty($result[$i]['value']['Sagskategori']['kategori'])){
echo $result[$i]['value']['Sagskategori']['kategori'];
}else{
echo "Intet fundet.";
}
echo "<hr>";
}
The json code can be find here: http://oda.ft.dk/api/Sag?$select=titel,Sagskategori/kategori&$expand=Sagskategori
Can anyone of you see my fail in the code, and get me on the right way :-) ?
$ito-1and then increment it as first thing in yourforeachto make is0? That's partially logical. Why not init it as0and then increment as last operation in yourforeach?var_dump($result)orprint_r($result)before you do yourforeach()loop to see what your result variable looks like. The main thing I'd first check is that you're getting back an iterative object. I think, you're just going to get back a single object so your entire loop idea might not be the right approach. The first value in your JSON isn't an array. edit I see a couple answers along these lines already. Upvotes for those. :)