Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Murat KAYA
10,127 PointsObjective-C Json Fetch And Parse Data
Hi,
I'm trying to parse data from my api.My code is correct.I used it a lot of time.But I didn't get any error like this.What I am doing wrong.Can you help me ?
-(void)getStatu {
NSURL *urlPath = [NSURL URLWithString:@"http://myurl.com/m/cc/cc_today.php"];
NSData *jsonData = [NSData dataWithContentsOfURL:urlPath];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);
_ccStatus = [NSMutableArray array];
NSArray *statuArray = [dataDictionary objectEnumerator];
for (NSDictionary *statuDictionary in statuArray) {
CCStatu *status = [CCStatu statuWithTitle:[statuDictionary objectForKey:@"gelen"]];
status.cevap = [statuDictionary objectForKey:@"cevap"];
status.cort = [statuDictionary valueForKeyPath:@"cort"];
status.kayip = [statuDictionary valueForKeyPath:@"kayip"];
status.lort = [statuDictionary valueForKeyPath:@"lort"];
status.tekil = [statuDictionary valueForKey:@"tekil"];
[_ccStatus addObject:status];
}
}
This is my json
2015-08-13 23:54:41.362 CSGB[3215:209292] {
cevap = "37,627";
cort = "00:00:48";
gelen = "54,247";
kayip = "16,620";
lort = "00:01:17";
sl = "46.30 %";
tekil = "3,316";
}
And this is error
2015-08-13 23:54:58.330 CSGB[3215:209292] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fcc12451c30
2015-08-13 23:54:58.336 CSGB[3215:209292] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fcc12451c30'
1 Answer
Murat KAYA
10,127 PointsIts happened because I have single dictionary.
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);
_ccStatus = [NSMutableArray array];
CCStatus *status = [CCStatu statuWithTitle:dataDictionary[@"gelen"]];
status.cevap = dataDictionary[@"cevap"];
status.cort = dataDictionary[@"cort"];
status.kayip = dataDictionary[@"kayip"];
status.lort = dataDictionary[@"lort"];
status.tekil = dataDictionary[@"tekil"];
[_ccStatus addObject:status];