【问题标题】:How to decode json data from post method for android app in php?如何从 php 中的 android 应用程序的 post 方法解码 json 数据?
【发布时间】:2015-08-14 20:00:09
【问题描述】:

我正在开发一个应用程序,我在其中传递凭据,然后通过 post 方法传递它。在这个页面中,我想访问登录页面中填写的 regno 和 pass 值。但我没有得到价值观。我尝试将值回显为 $data->regno,但我得到了 Null 值。请告诉我我做错了什么。

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

?>

当我发布凭据时,尽管从数据库中发布了正确的凭据,但我还是收到了这个:

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

【问题讨论】:

    标签: php arrays json post urldecode


    【解决方案1】:

    使用这个..

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

    返回有效的 json 字符串是

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

    这是一个简单的例子,供您理解。 在您的查询中,您编码的对象不是数组,我不确定,但我认为这是您的问题 请试试这个...

    <?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 数组。
    【解决方案2】:

    在 json_encode() 函数中使用数组作为参数而不是对象。

    【讨论】:

    • 我用过,但仍然得到一个 [] 。我希望 $_POST 元素应该以 JSON 格式出现
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2017-07-21
    相关资源
    最近更新 更多