Skip to content

Instantly share code, notes, and snippets.

@ilyabrin
Last active December 14, 2015 23:48
Show Gist options
  • Select an option

  • Save ilyabrin/5167752 to your computer and use it in GitHub Desktop.

Select an option

Save ilyabrin/5167752 to your computer and use it in GitHub Desktop.
iOS work with JSON
/*
#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