PHP:Json解码错误

时间:2015-05-26 05:46:21

标签: php mysql json

我试图将json字符串解析为mysql但是我得到一个错误,说array_keys()期望参数1是数组,在第58行给出null。

<?php
function array_value_recursive($key , $arr){
$val = array();
array_walk_recursive($arr, function ($v, $k) use($key, &$val){
if($k == $key) array_push($val, $v);
        });
return count($val) > 1 ? $val : array_pop($val);
    }
// get tag
$tag = $_POST['tag'];

// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// response Array
$response = array("tag" => $tag, "error" => FALSE);

// check for tag type
if ($tag == 'register') {
    // Request type is Register new user

    $name = $_POST['UserName'];
    $phone = $_POST['Phone'];
    $date=$_POST['userAddedDate'];
    $userIsActive=$_POST['isUserActive'];


        // store user
        $user = $db->storeUser($name,$phone,$date,$userIsActive);
        if ($user!=null) {
            // user stored successfully
           $response["user"]["uid"] = $user["uid"];
            $response["user"]["UserName"] = $user["userName"];
            $response["user"]["Phone"] = $user["userPhone"];
            $response["user"]["isUserActive"] = $user["isUserActive"];
            $response["user"]["Register"] = true;


            echo json_encode($response);
        }
        else {

           $user1 = $db->UserExist($name,$phone);
            // user failed to store
            $response["user"]["uid"] = $user1["uid"];
            $response["user"]["Register"] = false;
            $response["error_msg"] = "user_exist";
            echo json_encode($response);
        }

}
else if($tag == 'CompleteTrip'){



    $trip= $_POST['Trip'];

    // $tripstartDate = $_POST['tripStartDate'];
    // $uid=$_POST['userID'];

    // $bumpGravity = $_POST['bumpGravity'];
    // $bumpLong=$_POST['bumpLong'];
    // $bumplat=$_POST['bumplat'];
    // $bumpTime=$_POST['bumpTime'];
    //$bump=$_POST['bump'];

    $arraykey=array_keys($trip);
    $tripcount=count($arraykey);

    for ($i=0; $i < $tripcount ; $i++) { 

        //  print_r($arr[$arraykey[$i]]);

        $tripDescription=array_value_recursive('tripDescription',    
 $trip[$arraykey[$i]]);
        $tripStartDate=array_value_recursive('tripStartDate', 
 $trip[$arraykey[$i]]);
        $userID=array_value_recursive('userID', $trip[$arraykey[$i]]);

        $bumpGravity=array_value_recursive('bumpGravity', 
 $trip[$arraykey[$i]]);
        $bumpLong=array_value_recursive('bumpLong', $trip[$arraykey[$i]]);
        $bumplat=array_value_recursive('bumplat', $trip[$arraykey[$i]]);
        $bumpTime=array_value_recursive('bumpTime', $trip[$arraykey[$i]]);

        $trip1 = $db-

  >addcompletetrip($tripDescription,$tripStartDate,$userID,$bumpGravity,
   $bumpLong,
  $bumplat,$bumpTime);


    }


    //$trip = $db->addcompletetrip($tripdescription,$tripstartDate,$uid,$bumpGravity,$bumpLong,$bumplat,$bumpTime);
    if ($trip1) {
        $response["trip"]["feedback"] = "true";
        echo json_encode($response);
    }
    else
    {
        $response["trip"]["feedback"] = "false";
        echo json_encode($response);

    }

}
else if ($tag == 'Trip') {

    $tripdescription = $_POST['tripDescription'];
    $tripstartDate = $_POST['tripStartDate'];
    $uid=$_POST['userID'];


        // store trip
        $trip = $db->tripstart($tripdescription,$tripstartDate,$uid);
        if ($trip) {
            // trip stored successfully
            $response["trip"]["Description"] = $trip["tripDescription"];
            $response["trip"]["startDate"] = $trip["tripStartDate"];
            $response["trip"]["userID"] = $trip["userID"];
            $response["trip"]["tripID"] = $trip["tripID"];



            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = false;
            $response["error_msg"] = "Error occured in Registartion";
            echo json_encode($response);
        }

   }
   else if ($tag == 'Bump') {
    // Request type is Register new user
    $tripID = $_POST['tripId'];
    $bumpGravity = $_POST['bumpGravity'];
    $bumpLong=$_POST['bumpLong'];
    $bumplat=$_POST['bumplat'];
    $bumpTime=$_POST['bumpTime'];


        // store trip
        $trip = $db-
    >addbumps($tripID,$bumpGravity,$bumpLong,$bumplat,$bumpTime);
        if ($trip) {
            // trip stored successfully

            $response["bump"]["feedback"] = "true";



            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = false;
            $response["error_msg"] = "Error occured in Registartion";
            echo json_encode($response);
        }

    }
     else if ($tag == 'LeaderBoard') {

        // store trip
        $LBresponse = $db-
  >addbumps($tripID,$bumpGravity,$bumpLong,$bumplat,$bumpTime);
        if ($LBresponse) {


            echo json_encode($LBresponse);
        } else {
            // user failed to store
            $response["error"] = false;
            $response["error_msg"] = "Error occured in during data 
       fetching";
            echo json_encode($response);
        }

      }
      else if ($tag == 'userHistory') {

       $uid=$_POST['userID'];


        // store trip
        $history = $db->showhistory($uid);

         if ($history) {


            echo json_encode($history);
        } else {
            // user failed to store
            $response["error"] = false;
            $response["error_msg"] = "Error occured in during data 
      fetching";
            echo json_encode($response);
        }



         }
       else {
       // user failed to store
    $response["error"] = false;
    $response["error_msg"] = "error in saving data";
    echo json_encode($response);
    }

   ?>

这是我的json字符串

{
  "tag" : "CompleteTrip",
  "Trip" : [
    {
      "tripDescription" : "My Trip number 17",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089104",
          "bumplat" : "33.732934",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.5719842483606042
        },
        {
          "bumpLong" : "73.089104",
          "bumplat" : "33.732934",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.4080177753918094
        },
        {
          "bumpLong" : "73.089104",
          "bumplat" : "33.732934",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.448878156374468
        }
      ],
      "tripStartDate" : "20.05.2015"
    },
    {
      "tripDescription" : "My Trip number 18",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.6059529999867896
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.4395928505184715
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.4675621285346855
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "20.05.2015",
          "bumpGravity" : 0.6471158319159812
        }
      ],
      "tripStartDate" : "20.05.2015"
    },
    {
      "tripDescription" : "My Trip number 19",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089216",
          "bumplat" : "33.733184",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.6152545665366322
        },
        {
          "bumpLong" : "73.089216",
          "bumplat" : "33.733184",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.5464117459805118
        },
        {
          "bumpLong" : "73.089216",
          "bumplat" : "33.733184",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.5087776494496044
        }
      ],
      "tripStartDate" : "21.05.2015"
    },
    {
      "tripDescription" : "My Trip number 20",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089113",
          "bumplat" : "33.732939",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.7385709255522828
        },
        {
          "bumpLong" : "73.089113",
          "bumplat" : "33.732939",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.607726128634555
        },
        {
          "bumpLong" : "73.089113",
          "bumplat" : "33.732939",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.5887302539592243
        },
        {
          "bumpLong" : "73.089113",
          "bumplat" : "33.732939",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.4596315163659364
        }
      ],
      "tripStartDate" : "21.05.2015"
    },
    {
      "tripDescription" : "My Trip number 21",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.6060029844771418
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.4437488421895869
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.4253806333440103
        },
        {
          "bumpLong" : "73.089105",
          "bumplat" : "33.732965",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.7034483457347098
        }
      ],
      "tripStartDate" : "21.05.2015"
    },
    {
      "tripDescription" : "My Trip number 22",
      "userID" : "35",
      "bump" : [
        {
          "bumpLong" : "73.089067",
          "bumplat" : "33.732891",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.5309099143204541
        },
        {
          "bumpLong" : "73.089014",
          "bumplat" : "33.732961",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.4918705089435963
        },
        {
          "bumpLong" : "73.089067",
          "bumplat" : "33.732891",
          "bumpTime" : "21.05.2015",
          "bumpGravity" : 0.6217900667121954
        }
      ],
      "tripStartDate" : "21.05.2015"
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

因为array_keys()想要一个数组而$ trip是一个(json-)字符串。 在使用json-data之前,必须先调用json_decode()