2

I am getting NULL for all my JSON data here is my code

$jsons1 = file_get_contents($api_url); // returns JSON
        $jsons=json_decode($jsons1);

        foreach ($jsons as $json)
        {
        echo "\n";
        echo "<ul>";
        echo "<li>";
        echo "\nTitle : ".$json->title."\n";
        echo "\nContent: ".$json->content."\n";
        echo "</li>";
        echo "</ul>";
        }

I am unable to fetch it like $json->title, Am i missing something?

The output is

 - Title : Content: 

Please help in this regard

here is output of $jsons1 variable

{"results":[{"title": "Temp, Russia - Wikipedia, the free encyclopedia","kwic": "Coordinates : 52°03′N 39°44′E  /  52.05°N 39.733°E  / 52.05; 39.733  tenmp ( Russian : УÑмань ) is a town and the administrative center of ...","content": "","url": "http://en.wikipedia.org/wiki/sdfds,_Russia","iurl": "","domain": "en.wikipedia.org","author": "","news": false,"votes": "1","date": 1357744155518,"related":[]}],"query": "tenp","suggestions":[],"count":96,"start":1,"length":10,"time": "412"}
5
  • 3
    post the json string to ur question. Commented Apr 10, 2014 at 14:45
  • 2
    Also, print_r your $jsons variable to see if you're getting the array you think you're getting. Commented Apr 10, 2014 at 14:47
  • @Kryten I am getting response, and i believe it json_decode will work Commented Apr 10, 2014 at 14:50
  • Meh, it is lame to call decoded objects as $jsons or $json. Anything that is named as JSON is supposed to be a JSON string. All decoded stuff is objects. Commented Apr 10, 2014 at 14:54
  • @user1765876 - I wasn't asking if json_decode will work, I was suggesting you use print_r to see the structure of your object. I did a quick check of the JSON document you posted, and from the structure of the decoded object, it's clear you're referring to the wrong property in your for loop. Commented Apr 10, 2014 at 14:57

2 Answers 2

2

Try as

$j = '{"results":[{"title": "Temp, Russia - Wikipedia, the free encyclopedia","kwic": "Coordinates : 52°03′N 39°44′E  /  52.05°N 39.733°E  / 52.05; 39.733  tenmp ( Russian : УÑмань ) is a town and the administrative center of ...","content": "","url": "http://en.wikipedia.org/wiki/sdfds,_Russia","iurl": "","domain": "en.wikipedia.org","author": "","news": false,"votes": "1","date": 1357744155518,"related":[]}],"query": "tenp","suggestions":[],"count":96,"start":1,"length":10,"time": "412"}';

$data = json_decode($j,true);

foreach ($data["results"] as $key=>$val){
    echo "\n";
    echo "<ul>";
    echo "<li>";
    echo "\nTitle : ".$val["title"]."\n";
    echo "\nContent: ".$val["content"]."\n";
    echo "</li>";
    echo "</ul>";
}

Right now there is only one data in results element so looping is better if there are more.

Sign up to request clarification or add additional context in comments.

Comments

1

You have object results containing your datas..

So you have just to fix your Foreach statement ..>

    foreach ($jsons->results as $json) { ... }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.