我试图通过Android应用程序发布JSON,然后将JSON解码为Array,但得到:未定义索引:第6行上的usersJSON []
//Get JSON posted by Android Application
$json = $_POST["usersJSON"]; // Undefined index
php脚本如下所示:
<?php
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = $_POST["usersJSON"]; // Undefined index
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into MySQL DB
$res = $db->storeUser($data[$i]->userId,$data[$i]->userName);
//Based on inserttion, create JSON response
if($res){
$b["id"] = $data[$i]->userId;
$b["status"] = 'yes';
array_push($a,$b);
}else{
$b["id"] = $data[$i]->userId;
$b["status"] = 'no';
array_push($a,$b);
}
}
//Post JSON response back to Android Application
echo json_encode($a);
?>
最佳答案
看起来$_POST["usersJSON"]
没有设置或为空(您没有该索引的值)
检查您是否在url上发布
usersJSON='yourjsondata'
对于隐藏未定义索引错误,您需要先通过
isset()
或empty()
进行检查$json = (isset($_POST["usersJSON"])? $_POST["usersJSON"] : '');
或检查是否为空
if(!empty($_POST["usersJSON"]) {
$json = $_POST["usersJSON"];
}
else {
echo 'getting blank';
}