简体   繁体   中英

how to decode json data in php file

I want to decode json data which come from android like

["{\"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. This my php code which get json data and decode then save in database.I tried but but store null values(blank). 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.
This my php code which get json data and decode then save in database

    <?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.

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

Edit

Your problem is that your json string is incorrect. I had to decode it twice to make it work.

// 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"
  }
}

You can access your request by looking at the $_POST variable

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.

Display it using var_dump($_POST);

Example

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

Resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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