Keeping KEYS in correct ORDER in NSDictionary/NSArray from JSON

前端 未结 2 1181
-上瘾入骨i
-上瘾入骨i 2021-01-22 10:13

I have a JSON array returning from a request.
Data is as such (it comes from external source and cannot be changed from server):

[{\"clients\":
{\"name\":\"         


        
相关标签:
2条回答
  • 2021-01-22 11:03

    First, the exception you're seeing is because the selector name is incorrect. You should use keysSortedByValueUsingComparator. Second, you can't sort the keys, because the order in which you're getting them from the server doesn't seem to be in any particular order. If you MUST have them in that order, then send them from the server with a tag ("index") and then sort by this tag.

    0 讨论(0)
  • 2021-01-22 11:05

    Not only is NSDictionary in principle unordered, but so are JSON objects. These JSON objects are identical:

    [{"clients":
    {"name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com","website":"www.clientname.com"}
    }]
    
    [{"clients":
    {"website":"www.clientname.com","name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com"}
    }]
    

    If the server wants to have ordered keys, it needs to send an array. Of course you can just use the keysSortedByValueUsingComparator: method to get the keys in a sorted order.

    0 讨论(0)
提交回复
热议问题