2015-08-13 75 views
0

我試圖從我的api解析數據。我的代碼是正確的,我用了很多時間。但是我沒有得到像這樣的任何錯誤。我在做什麼錯了,你能幫助我嗎?Objective-C Json讀取和解析數據

-(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]; 
    } 

} 

這是我的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"; 
} 

這是錯誤

2015-08-13 23:54:58.330 APP[3215:209292] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fcc12451c30 
2015-08-13 23:54:58.336 APP[3215:209292] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fcc12451c30' 

回答

1

您的代碼錯誤地迭代數據。你只有一本字典。沒有什麼可迭代的。

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]; 
+0

非常感謝你,它的工作。 –