简体   繁体   English

在ios中以相同顺序解析来自服务器的JSON响应

[英]parsing JSON response from server in same order in ios

i have an app which fetches JSON response from server. 我有一个从服务器获取JSON响应的应用程序。 the JSON response from server looks as follows: 来自服务器的JSON响应如下所示:

{"status":"SUCCESS","message":"XYZ","token":"ABCDEFGHIJ"}

now i need to store this in a NSDictionary for further parsing. 现在我需要将其存储在NSDictionary中以进行进一步解析。 so i use the following approach: 所以我使用以下方法:

    urldata1=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&res   error:nil]; NSDictionary
    *myDictionary=[NSJSONSerialization JSONObjectWithData:urldata1 options:NSJSONReadingMutableContainers error:nil];
 }

but now the dictionary i get looks as follows: 但是现在我得到的字典如下:

{
    message = "XYZ";
    status = SUCCESS;
    token = "ABCDEFGHIJ";
}

So i see that the dictionary has been sorted on the basis of keys... is there a way to reproduce the exact same response from server in my dictionary.. 所以我看到字典是根据键排序的...有没有办法从我的字典中的服务器重现完全相同的响应。

It doesn't matter in what order the NSDictionary is because you retrieve the object from the dictionary with keys. NSDictionary顺序无关紧要,因为您可以使用键从字典中检索对象。 So if you want to access the status first use this code 因此,如果要访问状态,请首先使用此代码

NSString *status  = [myDictionary objectForKey@"status"];
NSString *message = [myDictionary objectForKey@"message"];
NSString *token = [myDictionary objectForKey@"token"];

And you can access a Dictionary inside a Dictionary like this 您可以像这样在字典中访问字典

NSDictionary *dict= [myDictionary objectForKey@"SomeOtherDictionary"];

Sorting a dictionary is meaningless. 对字典进行排序是没有意义的。 You need to first create an array which will sort according to your needs. 您首先需要创建一个数组,该数组将根据需要进行排序。

You can refer Sorting NSDictionary from JSON for UITableView for further explanations. 您可以参考UITableView的JSON的NSDictionary排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM