[英]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. 我正在开发一个应用程序,我在其中传递凭据,然后通过post方法传递它。 In this page, I want to access the regno and pass value that were filled in the login page. 在此页面中,我想访问登录页面中填写的regno和pass值。 But I am not getting the values. 但我没有得到价值观。 I tried echoing the values as $data->regno, but I got Null value. 我尝试将值回显为$ data-> regno,但我得到了Null值。 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 返回有效的json sting是
{"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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.