我有一個PHP文件,它爲某些類擦除了一個外部URL,然後將它們放入一個數組中。然後我使用json_encode()對數組進行編碼。該數組的順序必須迭代,但由於某種原因,JSON不會以正確的順序迭代。它按降序排列,而不是升序排列。jQuery。每個對象的排序
這裏是JSON的例子返回:
[{
"id":1,
"info":
{
"title":"Design Prepaid cards with HD quality ",
"titleurl":"http:\/\/www.peopleperhour.com\/job\/design-prepaid-ards-with-hd-quality-380258",
"price":"\u00a3 400 ",
"urgent":"Urgent",
"jobID":"380258"
}
},
{
"id":2,
"info":
{
"title":"Charted accontant",
"titleurl":"http:\/\/www.peopleperhour.com\/job\/charted-accontant-380251",
"price":"\u00a3 60 ",
"urgent":"Urgent",
"jobID":"380251"
}
}]
這是我當前如何顯示JSON:
var jsonResults = JSON.parse(data);
var count = (jsonResults.length);
// Iterate Through Results
$.each(jsonResults, function(key, value)
{
// Display Data
$('#resultsPanel').fadeIn('slow');
$('#resultsPanel').prepend(
'<div class="item" id="'+ value.info.jobID +'">'+
'<div class="title"><a href="'+ value.info.titleurl+'" target="_blank">'+ value.info.title +'</a></div>'+
'<div class="price">'+ value.info.price +'</div>'+
'</div>'
);
});
是否有可能訂購JSON?
您可以排序數組,如果你想有一定的順序包含的數組,但對象不能被排序,因爲在對象中不能保證順序。 – adeneo
作爲一個旁註,只需用'for(var i = jsonResult; i - ;)替換每個循環'{將向後迭代數組 – adeneo
我假設它與JSON對象中的數組的順序相同? –