2

I am not sure but it seems, when I de-serialize an object array using gson, the order is missed. For example, I have following json:

{   
    "Id": "b2340ca7-646b-44bd-8a35-8d3043ace98e", 
    "Arr2": [

        {
            "keyA1": "value1", 
            "keyA2": value2, 
            "keyA3": "value3"
        }, 
        {
            "keyB1": "value1", 
            "keyB2": value2, 
            "keyB3": "value3"
        }, 
        {
            "keyC1": "value1", 
            "keyC2": value2, 
            "keyC3": "value3"
        }
    ], 
    "key": "value", 
    "key": {
        "key1": "value1", 
        "key2": value2, 
        "key3": "value3"
    }, 
    "Arr1": [
        "Element 1", 
        "Element 2"
    ]
 }

After deserializing the order of elements in ARR2 is not same as in json above.

Benjamin Marwell
  • 1,173
  • 1
  • 13
  • 36
  • what do you mean by not the same? – Emil Adz Jan 08 '14 at 23:59
  • 1
    possible duplicate of [JSON order mixed up](http://stackoverflow.com/questions/3948206/json-order-mixed-up) – Austyn Mahoney Jan 09 '14 at 00:35
  • Lets say I call first object in ARR2 as object A, 2nd object as Object B and 3rd as Object C. After I de-serialize this array Arr2 into a HashLinkedSet using gson, I expect the order of elements in set to be Object A, Object B, Object C. But instead I get Object C, Object A, Object B. How can I maintain the order of elements from jsonArray after deserialization. – user3175491 Jan 09 '14 at 17:59
  • 3
    This is not duplicate of [JSON order mixed up](http://stackoverflow.com/questions/3948206/json-order-mixed-up) because that post talks about order of elements within json object. I need to maintain the order of objects after DE-SERIALIZATION of JSON ARRAY into linkedHashSet – user3175491 Jan 09 '14 at 18:03
  • It is a duplicate, it just talks about `java -> json`, which has the same lack of ordering specification as `json -> java'. My vote stands. – Austyn Mahoney Jan 14 '14 at 21:49
  • This seems to be a bug in your JSON serialization library, or possibly your code. The order of elements in an array is significant in JSON. What is *not* significant is the order of fields in an object. – Paolo Perrotta Sep 05 '14 at 11:02
  • @AustynMahoney It's not a duplicate, because http://www.json.org/ says: > An array is an ordered collection of values – Benjamin Marwell Aug 18 '15 at 13:05
  • 1
    @user3175491 please post the classes you are deserializing to. – Benjamin Marwell Aug 18 '15 at 13:43
  • Possible duplicate of [How to sort GSON Array based on a key?](https://stackoverflow.com/questions/18575757/how-to-sort-gson-array-based-on-a-key) – Paul Sweatte Jul 11 '17 at 16:59
  • did any one got the reason? – Don Jose Jan 22 '18 at 11:54

0 Answers0