Php JSON响应数组
我有这个php代码。如您所见,我通过函数showallevents查询mysql数据库。我将$result返回给$event变量。使用while循环,我将从事件中获得的值分配给响应数组,每次循环发生时,行都存储在数据数组中。我肯定在某个地方失败了,因为尽管我得到了正确数量的响应,但我在json上得到的所有值都是“null”。它还告诉我,关于JSONarray的一些信息无法转换为jsonobjectPhp JSON响应数组,php,android,arrays,json,Php,Android,Arrays,Json,我有这个php代码。如您所见,我通过函数showallevents查询mysql数据库。我将$result返回给$event变量。使用while循环,我将从事件中获得的值分配给响应数组,每次循环发生时,行都存储在数据数组中。我肯定在某个地方失败了,因为尽管我得到了正确数量的响应,但我在json上得到的所有值都是“null”。它还告诉我,关于JSONarray的一些信息无法转换为jsonobject if (isset($_POST['tag']) && $_POST['tag'
if (isset($_POST['tag']) && $_POST['tag'] != '')
{
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "success" => 0, "error" => 0);
// check for tag type
if ($tag == 'showallevents')
{
// Request type is show all events
// show all events
$event = $db->showallevents();
if ($event != false)
{
$data = array();
while($row = mysql_fetch_assoc($event))
{
$response["success"] = 1;
$response["uid"] = $event["uid"];
$response["event"]["date"] = $event["date"];
$response["event"]["hours"] = $event["hours"];
$response["event"]["store_name"] = $event["store_name"];
$response["event"]["event_information"] = $event["event_information"];
$response["event"]["event_type"] = $event["event_type"];
$response["event"]["Phone"] = $event["Phone"];
$response["event"]["address"] = $event["address"];
$response["event"]["created_at"] = $event["created_at"];
$response["event"]["updated_at"] = $event["updated_at"];
$data[]=$response;
}
echo json_encode($data);
}
else
{
// event not found
// echo json with error = 1
$response["error"] = 1;
$response["error_msg"] = "Events not found";
echo json_encode($response);
}
}
else
{
echo "Access Denied";
}
}
?>
DB_Functions.php
<?php
class DB_Functions
{
private $db;
//put your code here
// constructor
function __construct()
{
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct()
{
}
/**
* Select all events that are after yesterday.
*/
public function showallevents()
{
$result = mysql_query("SELECT * FROM events WHERE date >= CURDATE()");
return($result);
}
}
?>
您的
$response
变量是什么?
在PHP中,要创建关联数组,可以使用=>
而不是=
例如:
$array = ('key1' => 'value1', 'key2' => 'value2');
您已经合并了两段独立的代码,结果是一团混乱,结果不清楚 可以通过两种方式创建关联数组:
$array = (key=>value, key2=>value2);
您还可以使用:
$array[key]=value;
$array[key2]=value2;
请注意,“键”和“值”都只是变量;您可以在那里使用字符串,也可以从其他地方传入变量
在做类似的事情时,我使用以下方法:
$response["success"] = 1;
$response["uid"] = $event["uid"];
$response["event"]["date"] = $event["date"];
$response["event"]["hours"] = $event["hours"];
$response["event"]["store_name"] = $event["store_name"];
$response["event"]["event_information"] = $event["event_information"];
$response["event"]["event_type"] = $event["event_type"];
$response["event"]["Phone"] = $event["Phone"];
$response["event"]["address"] = $event["address"];
$response["event"]["created_at"] = $event["created_at"];
$response["event"]["updated_at"] = $event["updated_at"];
$data[]=$response;
如果您只想返回所有数组,可以使用mysql\u fetch\u assoc代替mysql\u fetch\u行吗
if ($tag == 'showallevents')
{
// Request type is show all events
// show all events
$event = $db->showallevents();
if ($event != false)
{
$data = array();
while($row = mysql_fetch_assoc($event))
{
$data[] = $row;
echo json_encode($data);
}
else
{
// event not found
// echo json with error = 1
$response["error"] = 1;
$response["error_msg"] = "Events not found";
echo json_encode($response);
}
}
else
{
echo "Access Denied";
}
}
?>
当我使用mysql_fetch_array命令时,它在$result处只返回1行。所以我得到了一些响应,但只有一行数据。@Fredrik感谢您指出我的错误,我更新了我的答案。使用此方法后,我设法得到了logcat 3响应,这应该是根据我执行的mysql查询应该得到的行数。。问题是它返回空值。它说jsonarray无法转换为jsonobject。猜测一下,检查showalleEvents()函数的结果。它可能没有返回您认为的结果。事实并非如此,因为我希望返回的行是正确的。为了让事情变得更简单,我在showallevents的主代码之后发布了Db_functions.php。确切地说,是什么在说jsonarray不能转换为jsonobject?我发布了一张带有json响应的图片,其中一张带有空结果,另一张带有错误消息。顺便说一下,不管结果如何,谢谢你的时间。
if (isset($_POST['tag']) && $_POST['tag'] != '')
{
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "success" => 0, "error" => 0);
// check for tag type
if ($tag == 'showallevents')
{
// Request type is show all events
// show all events
$event = $db->showallevents();
if ($event != false)
{
$data = array();
while($row = mysql_fetch_assoc($event))
{
$response["success"] = 1;
$response["uid"] = $event["uid"];
$response["event"]["date"] = $event["date"];
$response["event"]["hours"] = $event["hours"];
$response["event"]["store_name"] = $event["store_name"];
$response["event"]["event_information"] = $event["event_information"];
$response["event"]["event_type"] = $event["event_type"];
$response["event"]["Phone"] = $event["Phone"];
$response["event"]["address"] = $event["address"];
$response["event"]["created_at"] = $event["created_at"];
$response["event"]["updated_at"] = $event["updated_at"];
$data[]=$response;
}
echo json_encode($data);
}
else
{
// event not found
// echo json with error = 1
$response["error"] = 1;
$response["error_msg"] = "Events not found";
echo json_encode($response);
}
}
else
{
echo "Access Denied";
}
}
?>