1

I'am creating a search engine.In this search engine we search movies by actor's name . Searching movies through actor's name returns two different types of JSON data . If the actor name is not found then the JSON is returned in the following format (see the http://netflixroulette.net/api/api.php?actor=)

           {"errorcode":404,"message":"Sorry! We couldn't find any movies with that actor!"}

and when the actor name is found the JSON returned is multidimensional and is in the following format.(see the link:http://netflixroulette.net/api/api.php?actor=Yuki%20Kaji)

           [{"unit":883,"show_id":70299043,"show_title":"Attack on Titan","release_year":"2013","rating":"4.6","category":"Anime","show_cast":"Yuki Kaji, Yui Ishikawa, Marina Inoue, Daisuke Ono, Hiro Shimono, Hiroshi Kamiya, Keiji Fujiwara, Kish\u00f4 Taniyama, Romi Park, Ryota Ohsaka","director":"","summary":"For over a century, people have been living behind barricades to block out the giant Titans that threaten to destroy the human race. When a Titan destroys his hometown, young Eren Yeager becomes determined to fight back.","poster":"http:\/\/netflixroulette.net\/api\/posters\/70299043.jpg","mediatype":1,"runtime":"24 min"},                                                                 {                     "unit":17256,"show_id":80009097,"show_title":"Magi","release_year":"2012","rating":"3.8","category":"TV Shows","show_cast":"Kaori Ishihara, Erica Mendez, Yuki Kaji, Erik Scott Kimerer, Haruka Tomatsu, Cristina Valenzuela, Daisuke Ono, Matthew Mercer, Takahiro Sakurai, Lucien Dodge","director":"","summary":"A land of mysterious ruins and a magical treasure hunt await young Aladdin and his courageous friend Alibaba for the adventure of their lives.","poster":"http:\/\/netflixroulette.net\/api\/posters\/80009097.jpg","mediatype":0,"runtime":"N\/A"}]

I've tried this code

                    $output = json_decode($output);

                    if($output['errorcode']==400)
                    {
                        foreach($output as $key =>$row)
                        {
                            echo "<p>$key  :  $row";
                                    echo '<br>';
                        }
                    }
                    else
                    {
                        foreach($output as $value)
                        {

                            foreach($value as $key =>$row)
                            {
                                if($key == "mediatype" || $key == "runtime" || $key == "unit" || $key == "show_id" )
                                    continue;
                                else if($key == "show_cast" )
                                {
                                    echo"<br>Show Cast:";
                                    $pieces = explode(",", $row);
                                    foreach($pieces as $strings)
                                    {
                                        $link='http://localhost:8000/?Title=&director=&Actor='.$strings;
                                        echo "<li><a href='$link'>$strings</a>";
                                    }
                                    echo "<br>";
                                }
                                else if($key == "director" )
                                    {
                                        if(empty($row))
                                        echo"<br>Director:No details of director<br>";
                                        else
                                        {
                                            echo"<br>Director:";
                                            $link='http://localhost:8000/?Title=&director='.$row.'&Actor=';
                                            echo "<a href='$link'>$row</a><br>";
                                        }
                                    }
                                    else if($key !="poster")
                                    {
                                        echo "$key  :  $row";
                                            echo '<br>';
                                    }
                                    else
                                    {
                                        echo '<img src="'.$row.'" />';
                                    }
                            }

                            echo "<br><br><br><br><br><br>";
                        }
                    }

Using this gives me error of "Undefined index: errorcode" . Basically my question is what to use in if-else condition to differentiate between the two received JSON objects that are recieved.

Please help!! Thanks in advance..

1 Answer 1

2

Try if(is_object($output) && isset($output->errorcode)).

So it would be like this:

               $output = json_decode($output);

                if(is_object($output) && isset($output->errorcode))
                {
                    foreach($output as $key =>$row)
                    {
                        echo "<p>$key  :  $row";
                                echo '<br>';
                    }
                }
                else
                {
                    // the rest
                }

What's happening is the json is an object when there are no matches, but it is an array of objects when there are matches.

If we just use if(isset($output->errorcode)), then, in the case where there are matches, $output is an array and not an object, and the above attempts to treat it like an object, resulting in an alert about treating an array like an object.

The boolean && will "short circuit" when the first condition is false, and it will never evaluate the second condition. So we first check to see if it is an object with is_object().. if it isn't, then it will never check to see if it has a member called "errorcode", dodging the alert about treating an array as an object.

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

8 Comments

using if(isset($output['errorcode'])) is giving the following error "Cannot use object of type stdClass as array"
You should use $output->errorcode instead of $output['errorcode']
using $output['errorcode'] ,when i searched for an actor that exist is giving me the following error "Trying to get property of non-object" . But it is working fine when actor does not exist
I see what's happening. If you look at the JSON responses.. in the case of no match it is an object {"errorcode:404,etc}. When you do get matches, it is an array of objects: [{result1}{result2}{etc}]
Yeah , two different types of JSON are returned . How to differentiate between them????? Basically what shoud i write in if statement to differentiate between the two JSON??
|

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.