Parsejson Sorts My Json Data
I have a simple ajax call that looks like this: var data = jQuery.parseJSON(response.d); The response.d contents are: {'d':'[[{\'ExtensionData\':{},\'categoryId\':\'Help\'}],{\'11
Solution 1:
Retaining object key order between unserialisation and serialisation in JavaScript is never guaranteed. The only way to guarantee key order is to extract an object's keys and sort them according to a deterministic criteria, i.e. in order to guarantee order, you must use an array.
Edit:
A possible solution to your problem would be to include an array of the object keys in addition to your key-value collection (the original object) to your server response. By iterating over the ordered keys, you can access the object in the order you want.
E.g.
vardata = {
values: { /* your original object here */ },
/* keep a record of key order and include the keys as an array
in your response. That way you can guarantee order. */
keys: [11, 10, 7, 6, 12, 5, 4, 2, 1]
};
data.keys.forEach(function (key) {
var value = data.values[key];
/* do work here */
});
Solution 2:
jQuery.parseJSON();
does not not sort your objects. But some browsers do sorting your objects after rendering. For clarification see here
Post a Comment for "Parsejson Sorts My Json Data"