How to arrange object Propeties order same in Array using swift

57 Views Asked by At

I am trying to prepare Json Object in Swift

[
    {
        "property1": val1,
        "property2": val2,
        "property3": val3
    },
    {
        "property1": val1,
        "property2": val2,
        "property3": val3
    },
    {
        "property1": val1,
        "property2": val2,
        "property3": val3
    }
]

But when seing request the objects order is changed for array

[
    {
        "property1": val1,
        "property2": val2,
        "property3": val3
    },
    {
        "property1": val1,
        "property3": val3,
        "property2": val2
    },
    {
        "property3": val3,
        "property2": val2,
        "property1": val1
    }
]

How can we change same order for object in Swift.

1

There are 1 best solutions below

0
AyAz On

A dictionary is simply a container that can hold multiple data as key-value pair in an unordered way. so your JSON object always will be unordered.

but You can get in order if you retrieve it with key-value pair.

   print(object["property1"])
   print(object["property2"])
   print(object["property3"])