Ordered Dictionary in JSON

前端 未结 4 1707
梦毁少年i
梦毁少年i 2020-12-21 16:28

There are 3 string variables

public var userLoginId : String?
public var searchString : String?
public var tableName : String?

I have a dic

4条回答
  •  囚心锁ツ
    2020-12-21 16:51

    I know this was asked a year ago. But I recently ran into this issue while writing unit tests. Basically I wrote up a mock JSON dictionary, used JSONSerialization to turn that into data, and passed that into a parsing object of sorts. In this particular test, however, the order had to be maintained or the test would fail, and the previously described method didn't do that. So what worked for me is instead of starting with a dictionary, I start with a test JSON string and encode that into data using utf8. When it's parsed into a dictionary, the order of the JSON string is maintained in parsed dictionary.

提交回复
热议问题