无法将json从android插入到mysql中

时间:2015-02-13 06:49:09

标签: mysql json

一些帮助PLIZ。 我的Android应用程序发送一个类似

的json
[{"userName":"fred","userId":"1"},{"userName":"juma","userId":"2"},     {"userName":"drer","userId":"3"},{"userName":"drer","userId":"4"},{"userName":"sdd","userId":"5"}] 

到我的php 5.3服务器。当我在代码中使用字符串时,它在评论出json_encode后工作得很完美,但是当我使用file_get_contents时它什么也没做,当我取消注释json_encode它返回NULL时...我做错了什么。我的服务器主机使用的是php 5.3.28。这是我的代码..

<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions(); 

//Get JSON posted by Android Application
$json = file_get_contents('php://input');


//$json ='[{"userName":"fred","userId":"1"},{"userName":"juma","userId":"2"},{"userName":"drer","userId":"3"},{"userName":"drer","userId":"4"},{"userName":"sdd","userId":"5"}]';

//$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t](//).*)#", '', $json);

//$json = json_encode($json);     
$data = json_decode($json, true);

//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);


?>

错误是......

Notice: Uninitialized string offset: 0 in XXXX on line 28    

Notice: Trying to get property of non-object in xxxxxx on line 28

Notice: Uninitialized string offset: 0 in xxxxxx on line 28        

Notice: Trying to get property of non-object in xxxxxx on line 28

Notice: Uninitialized string offset: 0 in xxxxx on line 32    

Notice: Trying to get property of non-object in xxxxx on line 32
[{"id":null,"status":"yes"}]

NB ..第28行是

$res = $db->storeUser($data[$i]->userId, $data[$i]->userName);

和第32行是

$b["id"] = $data[$i]->userId;

当我回显$ json时,这是我的logcat的一部分

    49-949/com.qnatz.ppolocations I/System.out﹕ [{"userName":"sorry","userId":"1"},{"userName":"wakeup","userId":"2"},{"userName":"sleeping","userId":"3"}]
02-13 12:32:17.110      949-949/com.qnatz.ppolocations W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-13 12:32:18.070      949-949/com.qnatz.ppolocations I/System.out﹕ usersJSON=%5B%7B%22userName%22%3A%22sorry%22%2C%22userId%22%3A%221%22%7D%2C%7B%22userName%22%3A%22wakeup%22%2C%22userId%22%3A%222%22%7D%2C%7B%22userName%22%3A%22sleeping%22%2C%22userId%22%3A%223%22%7D%5D
02-13 12:32:18.100      949-949/com.qnatz.ppolocations W/System.err﹕ org.json.JSONException: Value usersJSON of type java.lang.String cannot be converted to JSONArray
02-13 12:32:18.100      9

1 个答案:

答案 0 :(得分:0)

使用此代码解决了......

<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions(); 

//Get JSON posted by Android Application
//$json = file_get_contents('php://input');

$json = urldecode(utf8_decode(stripslashes($_REQUEST['usersJSON'])));

//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);

?>