JSON输出开头的不需要的{}

时间:2015-03-18 11:01:33

标签: php json

我在PHP代码的json输出的开头得到{}。怎么消除这个?从android中的php访问此JSON输出。当android代码执行时,它显示从php代码中获取数据时出错。

这是我的PHP代码。

        <?php

        include_once "/db_connect.php";
        $db = new DB_CONNECT();
        $head='0';

        if(empty($head)){

            for($head=1;$head<20;$head++){

                if(!empty($head)){

                $response["abc"] = array();


            $flatp=mysql_query("SELECT co.id,co.user_id,co.cat_id,co.flatP,hds.HeadName,co.offer_name,u.shop_name,co.cost FROM abc co INNER JOIN categories ct ON ct.id=co.cat_id INNER JOIN 
                xyz u ON u.id=co.user_id INNER JOIN heads hds ON hds.id=ct.Head_id 
                 WHERE ct.Head_id='$head' AND ct.id=co.cat_id AND co.user_id=u.id ORDER BY co.flatP DESC LIMIT 1");

        if (mysql_num_rows($flatp) >0) {


        while ($row = mysql_fetch_array($flatp)) {  
         $recp = array();

           $recp["HeadName"]= $row["HeadName"];
          $recp["offer_name1"]=$row["offer_name"];
         $recp["shop_name1"]=$row['shop_name'];
         $recp["cost1"]=$row['cost'];

        $head_id=$row['user_id'];
        //$recp["$head_id"]='user_id';

        $imageurl='../local_offers_app/trunk/offer_images/'.$head_id.'/'.$row['id'].'.jpg';

        $img=stripslashes($imageurl);
        array_push($response["abc"], $recp);

        }
        }

        $buyget=mysql_query("SELECT co.id,co.user_id,co.cat_id,co.offer_name,co.cost,u.shop_name, CASE WHEN buy>'get' THEN buy
                else 'get'  END as buy FROM abc co INNER JOIN categories ct ON ct.id=co.cat_id INNER JOIN xyz u ON u.id=co.user_id 
                WHERE ct.Head_id='$head' AND ct.id=co.cat_id ORDER BY co.buy DESC LIMIT 1");

        if(mysql_num_rows($buyget) >0) {

         while ($row = mysql_fetch_array($buyget)) {
            $recp = array();

        $recp["offer_name2"]=$row["offer_name"];
         $recp["shop_name2"]=$row['shop_name'];
         $recp["cost2"]=$row['cost'];

        array_push($response["abc"], $recp);

        }
        }

        $buygetamt=mysql_query("SELECT a.id,a.user_id,a.cat_id,a.offer_name,a.cost,u.shop_name,a.buyqnt, 
        SUM(IF(a.cost<b.cost,1,0)) A,
        SUM(IF(a.buyqnt>b.buyqnt,1,0)) B
        FROM abc a JOIN abc b ON a.cost<b.cost AND a.buyqnt>b.buyqnt
         LEFT OUTER JOIN abc c ON a.cost=c.cost AND
         a.id>c.id AND b.id<c.id INNER JOIN categories ct ON ct.id=a.cat_id INNER JOIN xyz u ON u.id=a.user_id WHERE 
         ct.Head_id='$head' AND c.id IS NULL GROUP BY a.cost ORDER BY buyqnt DESC LIMIT 1");

         if(mysql_num_rows($buygetamt) >0) {
        while ($row = mysql_fetch_array($buygetamt)) {
            $recp = array();
           $recp["offer_name3"]=$row["offer_name"];
         $recp["shop_name3"]=$row['shop_name'];
        $recp["cost3"]=$row['cost'];
        array_push($response["abc"], $recp);

        }
        }

        $buygetp=mysql_query("SELECT a.id,a.user_id,a.cat_id,a.buyqnt,a.buyqntP,a.offer_name,a.cost,u.shop_name, 
        SUM(IF(a.buyqnt<b.buyqnt,1,0)) A,
        SUM(IF(a.buyqntP>b.buyqntP,1,0)) B
        FROM abc a JOIN abc b ON a.buyqnt<b.buyqnt AND a.buyqntP>b.buyqntP LEFT OUTER
        JOIN abc c ON a.buyqnt=c.buyqnt AND a.id>c.id AND b.id<c.id INNER JOIN categories 
        ct ON ct.id=a.cat_id INNER JOIN xyz u ON u.id=a.user_id WHERE ct.Head_id='$head' AND c.id IS NULL GROUP BY 
        a.buyqntP ORDER BY buyqntP DESC LIMIT 1");

        if(mysql_num_rows($buygetp) >0) {

        while ($row = mysql_fetch_array($buygetp)) {

            $recp = array();
        $recp["offer_name4"]=$row["offer_name"];
         $recp["shop_name4"]=$row['shop_name'];
        $recp["cost4"]=$row['cost'];

        array_push($response["abc"], $recp);

        }

        }

        $upto=mysql_query("SELECT a.id,a.user_id,a.cat_id,a.offer_name,a.cost,u.shop_name,a.upto, 
        SUM(IF(a.cost<b.cost,1,0)) A,
        SUM(IF(a.upto>b.upto,1,0)) B
        FROM abc a JOIN abc b ON  a.upto>b.upto LEFT OUTER JOIN abc c 
        ON a.cost=c.cost AND a.id>c.id AND b.id<c.id INNER JOIN categories ct ON ct.id=a.cat_id
         INNER JOIN xyz u ON u.id=a.user_id WHERE ct.Head_id='$head' AND c.id IS NULL GROUP BY a.upto ORDER BY upto DESC LIMIT 1");

        if(mysql_num_rows($upto) >0) {

        while ($row = mysql_fetch_array($upto)) {
            $recp = array();
        $recp["offer_name5"]=$row["offer_name"];
        $recp["shop_name5"]=$row['shop_name'];
        $recp["cost5"]=$row['cost'];
        array_push($response["abc"], $recp);

        }
        }       
        }
            $object = (object) array_filter((array) $response);
            $result = json_encode($object);
            echo $result;
                }
            }


        ?>

3 个答案:

答案 0 :(得分:0)

假设你这样做了:

$object = (object) array_filter((array) $response); 
$result = json_encode($object); 
  1. 如果您的数组是关联的,那么您不需要在json编码之前转换为对象,因为json_encode会将关联数组始终放入对象中
  2. array_filter从数组中删除所有等于false的条目(空数组不为false)
  3. 所以你应该为你的过滤器添加一个自定义回调:

    function notemptyarray($var) {
        return is_array($var) && count($var) > 0;
    } 
    
    $arr = array_filter((array) $response, "notemptyarray");
    $result = json_encode($arr);
    

答案 1 :(得分:0)

解决。

删除了'object'并仅使用'array_filter'。

答案 2 :(得分:0)

这就是我所做的..

而不是

$ object =(object)array_filter((array)$ response); $ result = json_encode($ object);

我把它改成了......

$ object = array_filter((array)$ response);

        $result = json_encode($object);

        echo trim($result,'[ ]');

并且有效。