这是我的json数据,我尝试解码这个json并将数据存储在mysql数据库中。 我在php中尝试json解码。在我的php服务器端,json数据没有被解码,也没有存储在mysql数据库中。请帮我解决这个问题。
JSON DATA
{
"code":"0",
"message":"Success",
"events":[
{
"id":1,
"name":"test event",
"description":"test desc",
"date":"2014-06-22 00:00:00",
"location":"keelapalur",
"type":1,
"passcode":"123456",
"created":{
"date":"2014-06-08 17:05:12",
"timezone_type":3,
"timezone":"UTC"
},
"updated":{
"date":"2014-06-08 17:05:12",
"timezone_type":3,
"timezone":"UTC"
}
},
{
"id":2,
"name":"rtyr",
"description":"rtyr",
"date":"2014-06-22 00:00:00",
"location":"try",
"type":1,
"passcode":"123456",
"created":{
"date":"2014-06-12 09:26:31",
"timezone_type":3,
"timezone":"UTC"
},
"updated":{
"date":"2014-06-12 09:26:31",
"timezone_type":3,
"timezone":"UTC"
}
}
]
}
PHP代码
<?php
$response = array();
$res=array();
require_once __DIR__ . '/db_connect.php';
$json = file_get_contents('/insert.json');
if($json!=null){
$decoded=json_decode($json,true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if(sizeof($decoded["events"])>0)
{
for($i=0;$i>sizeof($decoded["events"]);$i++)
{
$id=$decoded["events"]["id"];
$name=$decoded["events"]["name"];
$date=$decoded["events"]["date"];
$desc=$decoded["events"]["description"];
$location=$decoded["events"]["location"];
$type=$decoded["events"]["type"];
$passcode=$decoded["events"]["passcode"];
$cdate=$decoded["events"]["created"]["date"];
$ctimzonety= $decoded["events"]["created"]["timezone_type"];
$ctimz=$decoded["events"]["created"]["timezone"];
$udate=$decoded["events"]["updated"]["date"];
$utimzonety= $decoded["events"]["updated"]["timezone_type"];
$utimz=$decoded["events"]["updated"]["timezone"];
$result = mysql_query("INSERT INTO events(userid,name,desc,date,loc,type,passcode,cdate,ctimezone_type,ctimezone,udate,utimezone_type,utimezone)
VALUES('$id,'$name','$desc','$date','$loc','$type','$passcode','$cdate','$ctimzonety','$ctimz','$udate','$utimzonety','$utimz')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully stored";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
}
?>
答案 0 :(得分:4)
确保您的数据库连接。尝试使用此代码100%它正在工作......
<?php
$response = array();
$res=array();
$json = file_get_contents('C:\Users\Devunne3\Desktop\insert.json');
if($json!=null){
$decoded=json_decode($json,true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if(is_array($decoded["events"]))
{
foreach($decoded["events"] as $events)
//for($i=0;$i>sizeof($decoded["events"]);$i++)
{
$id=$events["id"];
echo "<br />","userid:",$id,"<br />";
$name=$events["name"];
echo "name:",$name,"<br />";
$date=$events["date"];
echo "date:",$date,"<br />";
$desc=$events["description"];
echo "desc:",$desc,"<br />";
$location=$events["location"];
echo "loc:",$location,"<br />";
$type=$events["type"];
echo "type:",$type,"<br />";
$passcode=$events["passcode"];
echo "passcode:",$passcode,"<br />";
$cdate=$events["created"]["date"];
echo "cdate:",$cdate,"<br />";
$ctimzonety=$events["created"]["timezone_type"];
echo "ctimezone_type:",$ctimzonety,"<br />";
$ctimz=$events["created"]["timezone"];
echo "ctimezone:",$ctimz,"<br />";
$udate=$events["updated"]["date"];
echo "udate:",$udate,"<br />";
$utimzonety=$events["updated"]["timezone_type"];
echo "utimezone_type:",$utimzonety,"<br />";
$utimz=$events["updated"]["timezone"];
echo "utimezone",$utimz,"<br />";
echo"------------------------------------------------","<br />";
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("INSERT INTO events(userid,name,desc,date,loc,type,passcode,cdate,ctimezone_type,ctimezone,udate,utimezone_type,utimezone)
VALUES('$id','$name','$desc','$date','$location','$type','$passcode','$cdate','$ctimzonety','$ctimz','$udate','$utimzonety','$utimz')")or die("Insert Failed ".mysql_error());;
}// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully stored";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
?>