JSON Arrays and Sorting

老子叫甜甜 提交于 2019-12-07 21:55:07

问题


I have this json array which I have outlined below. I want to know how I could get all the strings under the "name" key only and place in a certain array to be sorted alphabetically by name and later split into further arrays in accordance to the first letter in the names. Any guide to carrying this out will be much appreciated, thanks. I am using the json kit via github and also NSJSONserialization.

 {
   "proj_name": "Ant",
   "id": 
      [
          {
             "name": "David"
          },
          {
             "name": "Aaron"
          }
      ]
 },
 {
    "proj_name": "Dax",
    "id": 
         [
           {
             "name": "Adrian"
           },
           {
             "name": "Dan"
           }
         ]
  }

回答1:


Here is sample that selects just names and sort them alphabetically. Replace responseData with your data object.

NSMutableArray *names = [[NSMutableArray alloc] init];

NSError* error;
NSArray* json = [NSJSONSerialization 
    JSONObjectWithData:responseData
    options:kNilOptions 
    error:&error];

for (NSDictionary *proj in json) {
    NSArray *ids = [proj objectForKey: @"id"];

    for (NSDictionary *name in ids)
    {
        [names addObject: [name objectForKey: @"name"];
    }
}

NSArray *sortedNames = [names sortedArrayUsingSelector: @selector(localizedCaseInsensitiveCompare:)];



回答2:


Go to http://json.bloople.net/ in this link you can see the structure of your JSON response.

from the above response i can see the response as follow:

Project name: Dax

id : 0 name : Adrian

  1  name : Dan

So you can use the NSjsonserialization class from Apple. No need to use JSON kit.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your URL"]]];
  NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSLog(@"url=%@",request);

id jsonObject = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:nil];

if ([jsonObject respondsToSelector:@selector(objectForKey:)])

    {
    Nsstring *projectname=[jsonObject objectForKey:@"proj_name"];
    NSArray *name_array=[jsonObject objectForKey:@"id"];

     NSLog(@"projectname=%@",projectname);
     NSLog(@"name_array=%@",name_array);
    }



回答3:


Assuming you've successfully parsed the JSON into an NSArray, you can simplify things pretty dramatically:

NSArray *names = [parsedArray valueForKeyPath:@"@distinctUnionOfArrays.id.name"];

The names array should now contain all of the names flattened into a single array. To sort them, you could then do:

NSArray *sortedNames = [names sortedArrayUsingDescriptors:@[[NSSortDescriptor 
                                      sortDescriptorWithKey:@"description" ascending:YES]]];

Or all at once:

  NSArray *sortedNames = [[parsedArray valueForKeyPath:@"@distinctUnionOfArrays.id.name"]
                          sortedArrayUsingDescriptors:@[[NSSortDescriptor 
                                                 sortDescriptorWithKey:@"description"
                                                             ascending:YES]]];

The sortedNames array would now contain:

<__NSArrayI 0x713ac20>(
Aaron,
Adrian,
Dan,
David
)


来源:https://stackoverflow.com/questions/14601229/json-arrays-and-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!