简体   繁体   中英

How to decode json data from post method for android app in php?

I am developing an app, in which I pass the credentials and then pass it through post method. In this page, I want to access the regno and pass value that were filled in the login page. But I am not getting the values. I tried echoing the values as $data->regno, but I got Null value. Please tell me what am I doing wrong.

<?php

    ini_set('display_errors', '0');
    error_reporting(0);
    require_once("include/db.php");
    class mysendclass{
    public $name="Invalid";
    public $priority=-1;
    public $url="";
    }
    class myrecclass{
    public $regno="";
    public $pass="";
    }
    $e=new mysendclass();
    $g=new myrecclass();
    if(isset($_POST))
    {
        $data=json_decode(file_get_contents('php://input'));
        foreach ($data AS $key => $value) $g->{$key} = $value;
        $query = "SELECT priority, name, url FROM users WHERE regno='{$data->regno}' AND pass='{$data->pass}' LIMIT 1";
        echo ($query);
        $res = mysql_query($query, $conn) or die(mysql_error());
        $found = mysql_fetch_array($res);
        echo ($found);
        if($found)
        {
                $e->name=$found['name'];
                $e->priority=$found['priority'];
                $e->url=$found['url'];//url

            }
            echo json_encode($e);
    }

?>

When I post the credentials, I receive this, inspite of posting correct credentials from the database:

{"name":"Invalid","priority":-1,"url":""}

use this..

<?php
$abc = array();
$abc['my_first_name'] = "Suresh";
$abc['my_last_name'] = "Kumar";
echo json_encode($abc);
?>

return valid json sting is

{"my_first_name":"Suresh","my_last_name":"Kumar"}

This is a simple example for your understanding.. In your query you encode a object not a array i am not sure but i think this is your prob please try this...

<?php

    ini_set('display_errors', '0');
    error_reporting(0);
    require_once("include/db.php");
    class mysendclass{
    public $name="Invalid";
    public $priority=-1;
    public $url="";
    }
    class myrecclass{
    public $regno="";
    public $pass="";
    }
$records = array();
    $e=new mysendclass();
    $g=new myrecclass();
    if(isset($_POST))
    {
        $data=json_decode(file_get_contents('php://input'));
        foreach ($data AS $key => $value) $g->{$key} = $value;
        $query = "SELECT priority, name, url FROM users WHERE regno='{$data->regno}' AND pass='{$data->pass}' LIMIT 1";
        echo ($query);
        $res = mysql_query($query, $conn) or die(mysql_error());
        $found = mysql_fetch_array($res);
        echo ($found);
        if($found)
        {
                $records['name']=$found['name'];
                $records['priority']=$found['priority'];
                $records['url']=$found['url'];//url

            }
            echo json_encode($records);
    }

?>

在json_encode()函数中使用array作为参数而不是object。

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