如何从服务器获取有效的json响应

时间:2015-02-16 10:03:37

标签: php json

嗨,在下面的代码中我得到了无效的json输出。如何得到正确的json响应。[{" groupname":" New"},{" groupname":" Group"}]这个输出只期待但是再来一次

我错了,我没有得到

[{"groupname":"New"}][{"groupname":"New"},{"groupname":"Group"}]

预期的产出就是这个:

[{"groupname":"New"},{"groupname":"Group"}]

PHP

case "DispalyGroupDetails":
        $userId = authenticateUser($db, $username, $password);
        $array = array();
        if ($userId != NULL)

        {

            if (isset($_REQUEST['username']))           
            {               
                 $username = $_REQUEST['username'];



                 $sql = "select Id from users where username='$username' limit 1";

                 if ($result = $db->query($sql))

                 {
                        if ($row = $db->fetchObject($result))

                        {    

                                     $sql = "SELECT g.groupname 
                                            FROM `users` u, `friends` f, `group` g 
                                            WHERE u.Id=f.providerId and f.providerId=g.providerId
                                                GROUP BY g.id, g.groupname";
                                    $theResult = $db->query($sql);



                        while( $theRow = $db->fetchObject($theResult))
                        {



                        $json_output[]=$theRow;

                         print(json_encode($json_output));


                        }

                                //$out = SUCCESSFUL;


                        }
                        else
                        {
                            //$out = FAILED;                        
                        }
                 }

                 else
                 {
                        //$out = FAILED;
                 }              
            }

            else
            {
                    //$out = FAILED;
            }           
        }
        else
        {
            //$out = FAILED;
        }   
    break;

1 个答案:

答案 0 :(得分:2)

将此行放在while循环

之后
 print(json_encode($json_output));