Swift json decoding loses json object key order

后端 未结 2 801
梦谈多话
梦谈多话 2021-01-07 03:44

I have a simple JSON object:

{
    \"values\": {
        \"a\":\"\",
        \"b\":\"\",
        \"c\":\"\",
        \"d\":\"\",
        \"e\":\"\"
    }
}
<         


        
2条回答
  •  我在风中等你
    2021-01-07 04:00

    The following does not work so you don't waste time trying:

    Back to the old fashion way, use JSONSerialization.jsonObject(with: Data, options: JSONSerialization.ReadingOptions) -> Any.

    It does keep order..... UNTIL you cast json?["values"] into a [String: Any]. At this time what Cezar says in the above answer enters the scene: dictionaries are unordered.

    The following screenshot shows that, until json?["values"] is an Any, the order is kept in the string description.

提交回复
热议问题