如何使用jquery getJSON以相反的顺序接收json数据

时间:2016-07-03 18:10:49

标签: php jquery html json ajax

我使用php api生成json数据,

$db_result_set = $db->prepare('SELECT p.Id, p.FirstName, p.MiddleName, p.LastName, p.Gender, p.Location, p.Email, p.Mobile,p.Message,b.BookTitle, b.BookGenre, b.BookWriter, b.BookDescription FROM personaldetails AS p LEFT JOIN bookdetails AS b ON b.UserId = p.Id ORDER BY p.Id DESC');;

$db_result_set->execute();

$final = [];
foreach ($db_result_set as $u) {

   if (!isset($final[$u["Id"]])) {
        $final[$u["Id"]]= [
            "Id" => $u["Id"],
            "FirstName" => $u["FirstName"],
            "MiddleName" => $u["MiddleName"],
            "LastName" => $u["LastName"],
            "Gender" => $u["Gender"],
            "Location" => $u["Location"],
            "Email" => $u["Email"],
            "Mobile" => $u["Mobile"],
            "Message" => $u["Message"],
         ];
    }
    $final[$u["Id"]]["books"][] = [
        "BookTitle" => $u["BookTitle"],
        "BookGenre" => $u["BookGenre"],
        "BookWriter" => $u["BookWriter"],
        "BookDescription" => $u["BookDescription"],
    ];

}   

    header('Content-type: application/json');
    echo json_encode($final,JSON_PRETTY_PRINT);

这个api的输出是;

{
"110": {
    "Id": "110",
    "FirstName": "",
    "MiddleName": "",
    "LastName": "",
    "Gender": null,
    "Location": "",
    "Email": "",
    "Mobile": "",
    "Message": "",
    "books": [
        {
            "BookTitle": "",
            "BookGenre": "",
            "BookWriter": "",
            "BookDescription": ""
        }
    ]
},
"109": {
    "Id": "109",
    "FirstName": "",
    "MiddleName": "",
    "LastName": "",
    "Gender": null,
    "Location": "",
    "Email": "",
    "Mobile": "",
    "Message": "",
    "books": [
        {
            "BookTitle": "",
            "BookGenre": "",
            "BookWriter": "",
            "BookDescription": ""
        }
    ]
},
"108": {
    "Id": "108",
    "FirstName": "",
    "MiddleName": "",
    "LastName": "",
    "Gender": null,
    "Location": "",
    "Email": "",
    "Mobile": "",
    "Message": "",
    "books": [
        {
            "BookTitle": "",
            "BookGenre": "",
            "BookWriter": "",
            "BookDescription": ""
        }
    ]
},
"107": {
    "Id": "107",
    "FirstName": "",
    "MiddleName": "",
    "LastName": "",
    "Gender": null,
    "Location": "",
    "Email": "",
    "Mobile": "",
    "Message": "",
    "books": [
        {
            "BookTitle": "",
            "BookGenre": "",
            "BookWriter": "",
            "BookDescription": ""
        }
    ]
},
"106": {
    "Id": "106",
    "FirstName": "",
    "MiddleName": "",
    "LastName": "",
    "Gender": null,
    "Location": "",
    "Email": "",
    "Mobile": "",
    "Message": "",
    "books": [
        {
            "BookTitle": "",
            "BookGenre": "",
            "BookWriter": "",
            "BookDescription": ""
        }
    ]
},
"105": {
    "Id": "105",
    "FirstName": "",
    "MiddleName": "",
    "LastName": "",
    "Gender": null,
    "Location": "",
    "Email": "",
    "Mobile": "",
    "Message": "",
    "books": [
        {
            "BookTitle": "",
            "BookGenre": "",
            "BookWriter": "",
            "BookDescription": ""
        }
    ]
},
"104": {
    "Id": "104",
    "FirstName": "",
    "MiddleName": "",
    "LastName": "",
    "Gender": null,
    "Location": "",
    "Email": "",
    "Mobile": "",
    "Message": "",
    "books": [
        {
            "BookTitle": "",
            "BookGenre": "",
            "BookWriter": "",
            "BookDescription": ""
        }
    ]

我想在html中以减少格式(即110,109,108 ---)显示数据。

但是当我使用jquery getJSON函数在html中获取这些json数据时。 它以递增的格式显示(即104,105,106,106,---)。

如何使用getJSON函数反转我在html中获取的数据。

html中的jquery代码;

function loadData(){
                    var url = "http://localhost/ReadExchange/api.php";
                    $.getJSON(url,function(json) {
                        console.log(json);

                    if(json) {
                        alert("Roger that"+JSON.stringify(json));
}



                });

3 个答案:

答案 0 :(得分:1)

订单可能会更改,因为Javascript对象没有订单。

我会摆脱PHP中的键,所以你只需要在Javascript中有一个数组,它与你的PHP脚本中的顺序相同:

在json_encode之前使用它:

$final = array_values($final)

array_values只获取数组值,并使用0以上的数字键创建一个新数组。

答案 1 :(得分:0)

您可以对返回的数据调用.get()。reverse()。

var sortedData = $(json).get().reverse();

请参阅https://jsfiddle.net/9sfb5e6j/

答案 2 :(得分:0)

以相反的方式循环遍历:

var kS = Object.keys(testJSON);

for (var i=kS.length-1; i>=0 ; i--) {

     console.log(testJSON[kS[i]]);

}