1

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 :-) ?

2
  • quick side note - you initialize $i to -1 and then increment it as first thing in your foreach to make is 0? That's partially logical. Why not init it as 0 and then increment as last operation in your foreach? Commented Jan 5, 2015 at 16:46
  • Try doing a var_dump($result) or print_r($result) before you do your foreach() 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. :) Commented Jan 5, 2015 at 16:51

2 Answers 2

1

Please replace

foreach ($result as $data) {

by

foreach ($result["value"] as $data) {

And now you can iterate to your value array and get all informations from $data You don't need use $i, $data contains correct $result[$i] value

foreach ($result["value"] as $data) {
    echo "#";
    echo "<br>";
    echo "<b>Test 1:</b>";
    echo "<br>";
    if(!empty($data['Sagskategori']['kategori'])){
        echo $data['Sagskategori']['kategori'];
    }else{
        echo "Intet fundet.";
    }
    echo "<hr>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

The JSON starts like this:

{
  "odata.metadata":"...snip...","value":[
{

So the array is inside the value object.

The correct code should be:

 foreach ($result['value'] as $data) {
    // snip
    if(!empty($result['value'][$i]['Sagskategori']['kategori'])){
        echo $result['value'][$i]['Sagskategori']['kategori'];
    }

Also, inside the loop, $result['value'][$i]['Sagskategori']['kategori']; is strictly the same as using $data['Sagskategori']['kategori'];.

1 Comment

Thank you so much :-)! it was a big help!

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.