Json按ID排序,将从数据库中获取

时间:2017-01-08 08:26:54

标签: php android html json

我遇到了JSON问题。我想按变量id的递增顺序对JSON进行排序。

这是目前的JSON数据

{"server_response":[{"id":"9","email":"test@gmail.com=","password":"test"},{"id":"5","email":"json@gmail.com","password":"json"},{"id":"14","email":"wrong@gmail.com","password":"test"},{"id":"13","email":"mail@gmail.com=","password":"mail"}]}

我正在使用的php文件是

<?php

require_once('dbConnect.php');

$sql = "select * from users;";

$result = mysqli_query($con,$sql);

$response = array();

while($row = mysqli_fetch_array($result))
{
 array_push($response,array("id"=>$row[0],"email"=>$row[1],"password"=>$row[2]));
}

echo json_encode(array("server_response"=>$response));

mysqli_close($con);

?>

但我希望我的输出如下所示。对于php文件???

的任何更改
{"server_response":[{"id":"5","email":"json@gmail.com","password":"json"},{"id":"9","email":"test@gmail.com=","password":"test"},{"id":"13","email":"mail@gmail.com=","password":"mail"},{"id":"14","email":"wrong@gmail.com","password":"test"}]}

2 个答案:

答案 0 :(得分:0)

您可以在客户端进行排序,但如果仅从服务器端进行排序会更好。你需要做的是:

$sql = "select * from users order by id;";

这应该有效。如果您想按降序排列,请添加关键字:&#39; desc&#39;在id之后(没有单引号)。

答案 1 :(得分:0)

尝试以下代码...

webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.waitForBackgroundJavaScript(15000);
webClient.waitForBackgroundJavaScriptStartingBefore(5000);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setUseInsecureSSL(true);