繁体   English   中英

如何解码php文件中的json数据

[英]how to decode json data in php file

我想解码来自android的json数据

["{\"User_Registartion_Details\":{\"fullname\":\"ssss\",\"emailid\":\"sumitbwm@gmail.com\",\"mobile\":\"8796485577\",\"location\":\"pune\"}}"]

怎么做....从json获取数据并使用php脚本尝试将其保存在我的sql数据库中,但在我的sql中显示空值。 这是我的php代码,它获取json数据并进行解码,然后保存在database.I尝试,但存储了空值(空白)。 数据以json格式从android页面获取并在php脚本中解码,保存在db..anyboday中对此有所了解,请help..i在android / php中是新的。
这是我的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);

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;

编辑

您的问题是您的json字符串不正确。 我不得不对其解码两次,以使其正常工作。

// Incorrect json string
$json = '["{\"User_Registartion_Details\":{\"fullname\":\"ssss\",\"emailid\":\"sumitbwm@g‌​mail.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"
  }
}

您可以通过查看$_POST变量来访问您的请求

当在请求中使用application / x-www-form-urlencoded或multipart / form-data作为HTTP Content-Type时,通过HTTP 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM