按 id 排序 json 输出
Sort json output by id
我正在创建一个将 json 提要解析为列表活动的应用程序。我读过在列表视图的底部添加了新项目,我的应用程序就是这种情况。我试图通过按 id 降序排序我的 json 的输出来解决这个问题。不知道这是否会解决我的问题,或者我需要在我的 java 代码中修复它。我的问题的第一部分是,更改数据库的输出顺序是否会更改提要在应用程序中的显示顺序?如果是这样,我尝试按照 http://php.net/manual/en/function.usort.php 上的示例修改我的 php 脚本,但顺序没有改变。第二部分,我在以下 php 脚本中做错了什么?
<?php
require("config.inc.php");
$query_params=null;
//initial query
$query = "Select * FROM feeds";
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
if ($rows) {
$response["feed"] = array();
foreach ($rows as $row) {
$post = array();
$post["id"] = $row["id"];
$post["name"] = $row["name"];
$post["status"] = $row["status"];
//update our repsonse JSON data
array_push($response["feed"], $post);
}
function cmp($a, $b)
{
if ($a->id == $b->id) {
return 0;
}
return ($a->id > $b->id) ? -1 : 1;
}
$a = array();
usort($a, "cmp");
// echoing JSON response
echo json_encode($response, JSON_NUMERIC_CHECK);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
?>
看起来您是在空数组上调用 usort,而不是您实际想要排序的数据。
而不是
$a = array();
usort($a, "cmp");
尝试
$a = $response['feed'];
usort($a, "cmp");
$response['feed'] = $a;
Blackbelt 是正确的,您不能从技术上对 JSON 数组进行排序,至少不能对具有顺序索引的数组进行排序(这会导致根本没有索引)。但实际上,当您在客户端遍历结果对象时,它将按照定义的顺序排列,因此在服务器端对其进行排序不会引起任何问题。
我正在创建一个将 json 提要解析为列表活动的应用程序。我读过在列表视图的底部添加了新项目,我的应用程序就是这种情况。我试图通过按 id 降序排序我的 json 的输出来解决这个问题。不知道这是否会解决我的问题,或者我需要在我的 java 代码中修复它。我的问题的第一部分是,更改数据库的输出顺序是否会更改提要在应用程序中的显示顺序?如果是这样,我尝试按照 http://php.net/manual/en/function.usort.php 上的示例修改我的 php 脚本,但顺序没有改变。第二部分,我在以下 php 脚本中做错了什么?
<?php
require("config.inc.php");
$query_params=null;
//initial query
$query = "Select * FROM feeds";
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
if ($rows) {
$response["feed"] = array();
foreach ($rows as $row) {
$post = array();
$post["id"] = $row["id"];
$post["name"] = $row["name"];
$post["status"] = $row["status"];
//update our repsonse JSON data
array_push($response["feed"], $post);
}
function cmp($a, $b)
{
if ($a->id == $b->id) {
return 0;
}
return ($a->id > $b->id) ? -1 : 1;
}
$a = array();
usort($a, "cmp");
// echoing JSON response
echo json_encode($response, JSON_NUMERIC_CHECK);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
?>
看起来您是在空数组上调用 usort,而不是您实际想要排序的数据。
而不是
$a = array();
usort($a, "cmp");
尝试
$a = $response['feed'];
usort($a, "cmp");
$response['feed'] = $a;
Blackbelt 是正确的,您不能从技术上对 JSON 数组进行排序,至少不能对具有顺序索引的数组进行排序(这会导致根本没有索引)。但实际上,当您在客户端遍历结果对象时,它将按照定义的顺序排列,因此在服务器端对其进行排序不会引起任何问题。