Last active
December 14, 2015 23:48
-
-
Save ilyabrin/5167752 to your computer and use it in GitHub Desktop.
iOS work with JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| #import "JSON.h" | |
| */ | |
| // SBJSON object | |
| SBJSON *parser = [[SBJSON alloc] init]; | |
| // URL to serviceTree.json | |
| NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://pw.net/serviceTree.json"]]; | |
| // Обработка запроса и JSON -> NSData | |
| NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; | |
| // JSON -> NSString из получившегося NSData | |
| NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; | |
| // перевод JSON в объект | |
| NSArray *statuses = [parser objectWithString:json_string error:nil]; | |
| // Каждый элемент то отдельная услуга (в виде NSDictionary) | |
| for (NSDictionary *service in services) { | |
| // Отдельные значения извлекаются через objectForKey для NSDictionary | |
| // ВКонсоли.Ru | |
| NSLog(@"%@ - %@", [code objectForKey:@"code"], [[service objectForKey:@"service"] objectForKey:@"service_description"]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment