Skip to content

Instantly share code, notes, and snippets.

@JohnnyTseng
Created April 30, 2015 06:47
Show Gist options
  • Save JohnnyTseng/30b1b4ea84c52440f91a to your computer and use it in GitHub Desktop.
Save JohnnyTseng/30b1b4ea84c52440f91a to your computer and use it in GitHub Desktop.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSString *originalString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSArray *array = [originalString componentsSeparatedByString:@"},"];
NSArray *json = [NSJSONSerialization JSONObjectWithData:[originalString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
NSMutableArray *keys = [[NSMutableArray alloc] init];
for (NSString *string in array) {
NSString *pattern = @"\"(.*)\"[:| :|: ]";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *match in matches) {
NSRange group1 = [match rangeAtIndex:1];
NSString *matchedKey= [string substringWithRange:group1];
[tempArray addObject:matchedKey];
}
[keys addObject:tempArray];
}
[json enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSArray *objectKeys = [keys objectAtIndex:idx];
NSDictionary *dictionary = (NSDictionary *)obj;
if (objectKeys.count == dictionary.allKeys.count) {
for (NSString *key in objectKeys) {
NSLog(@"Value: %@", [dictionary objectForKey:key]);
}
NSLog(@"-----");
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment