使用php区分两个不同的JSON对象

时间:2017-03-15 21:54:22

标签: php json

我正在创建一个搜索引擎。在这个搜索引擎中,我们按照演员的名字搜索电影。通过actor的名称搜索电影会返回两种不同类型的JSON数据。如果未找到actor名称,则以下列格式返回JSON(请参阅http://netflixroulette.net/api/api.php?actor=

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

当找到actor名称时,返回的JSON是多维的,并且格式如下。(参见链接: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"}]

我已尝试过此代码

                    $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>";
                        }
                    }

使用这个给我错误&#34;未定义的索引:错误代码&#34; 。基本上我的问题是在if-else条件下使用什么来区分收到的两个收到的JSON对象。

请帮忙!!提前谢谢..

1 个答案:

答案 0 :(得分:2)

尝试if(is_object($output) && isset($output->errorcode))

所以它会是这样的:

               $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
                }

当没有匹配时,json是一个对象,但是当匹配时它是一个对象数组。

如果我们只使用if(isset($output->errorcode)),那么,在 匹配的情况下,$ output是一个数组而不是一个对象,上面的尝试将它当作一个对象来对待,导致关于像对象一样处理数组的警报。

布尔值&&将&#34;短路&#34;当第一个条件为假时,它将永远不会评估第二个条件。所以我们首先检查它是否是is_object()的对象..如果它不是,那么它永远不会检查它是否有一个名为&#34;错误代码&#34;的成员,躲避关于将数组视为对象的警告。