[英]how to decode json data in php file
I want to decode json data which come from android like 我想解码来自android的json数据
["{\"User_Registartion_Details\":{\"fullname\":\"ssss\",\"emailid\":\"sumitbwm@gmail.com\",\"mobile\":\"8796485577\",\"location\":\"pune\"}}"]
how to do it....get data from json and save in my sql db suing php script trying but in my sql show null values. 怎么做....从json获取数据并使用php脚本尝试将其保存在我的sql数据库中,但在我的sql中显示空值。 This my php code which get json data and decode then save in database.I tried but but store null values(blank).
这是我的php代码,它获取json数据并进行解码,然后保存在database.I尝试,但存储了空值(空白)。 Data get from android page in json format and decode in php script the save in db..anyboday have idea about this please help..i new in android/php.
数据以json格式从android页面获取并在php脚本中解码,保存在db..anyboday中对此有所了解,请help..i在android / php中是新的。
This my php code which get json data and decode then save in database 这是我的php代码,它获取json数据并进行解码,然后保存在数据库中
<?php
// Include confi.php
include_once('conection.php');
if($_SERVER['REQUEST_METHOD'] == "POST" && $_SERVER["CONTENT_TYPE"] == "application/json")
{
$request = file_get_contents('php://input');
$array = json_decode($request,true);
$name = $array['fullname'];
$email = $array['emailid'] ;
$mobileno = $array['mobile'] ;
$location = $array['location'];
$sql = "INSERT INTO usertable (`fullname`, `emailid`, `mobile`, `location`) VALUES ('$array[name]', '$array[email]', '$array[mobileno]', '$array[location]')";
$qur = mysql_query($sql);
if($qur)
{
$json = array("status" => 1, "msg" => "Done User added!");
}
else
{
$json = array("status" => 0, "msg" => "Error adding user!");
}
}else
{
$json = array("status" => 0, "msg" => "Request method not accepted");
}
@mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json);
JSON Encoding and Decoding in PHP is done using the json_encode
and `json_decode``functions. PHP中的JSON编码和解码是使用
json_encode
和`json_decode``函数完成的。
// To encode
$encodes = json_encode($json);
// To decode
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$decoded = json_decode($json);
// To see which variables are inside your decoded json
echo "<pre>";
var_dump($decoded);
echo "</pre>";
exit;
Your problem is that your json string is incorrect. 您的问题是您的json字符串不正确。 I had to decode it twice to make it work.
我不得不对其解码两次,以使其正常工作。
// Incorrect json string
$json = '["{\"User_Registartion_Details\":{\"fullname\":\"ssss\",\"emailid\":\"sumitbwm@gmail.com\",\"mobile\":\"8796485577\",\"location\":\"pune\"}}"]';
// Correct Json string
$json = '{"User_Registartion_Details":{"fullname":"ssss","emailid":"sumitbwm@g‌​mail.com","mobile":"8796485577","location":"pune"}}';
echo "<pre>";
var_dump(json_decode($json));
object(stdClass)#25 (1) {
["User_Registartion_Details"]=>
object(stdClass)#19 (4) {
["fullname"]=>
string(4) "ssss"
["emailid"]=>
string(24) "sumitbwm@g‌​mail.com"
["mobile"]=>
string(10) "8796485577"
["location"]=>
string(4) "pune"
}
}
You can access your request by looking at the $_POST
variable 您可以通过查看
$_POST
变量来访问您的请求
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
当在请求中使用application / x-www-form-urlencoded或multipart / form-data作为HTTP Content-Type时,通过HTTP POST方法传递给当前脚本的变量的关联数组。
Display it using var_dump($_POST);
使用
var_dump($_POST);
// Your android application sends a request
// This request is part of the HTTP protocol
// It is either a POST, GET, PUT or DELETE request
// PHP handles PUT and DELETE as a POST request
// The request data will be stored in the $_POST varaible and the $_REQUEST variable
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Use a var_dump to learn what key holds the json data
var_dump($_POST);
// let's say its in $_POST['json']
$json = $_POST['json'];
var_dump(json_decode($json));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.