2016-02-27 50 views
0

我根據學校的菜單和菜品開發一個應用,在那裏我堅持在解析複雜/嵌套JSON解析複雜的JSON(瀏覽它產生對象的層次結構)的目標C

樣品的Json網址: Json Data

到目前爲止,我剛做這爲使用SBJSON在下面的代碼中提到:

dispatch_async(dispatch_get_main_queue(),^ { 

    Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [reachability currentReachabilityStatus]; 
    if (networkStatus == NotReachable) { 
     [self.view setUserInteractionEnabled:YES]; 
     [HUD hideUIBlockingIndicator]; 

    } 

    else 
    { 

     Url =[NSString stringWithFormat:@"http://private-e8e699-yumyummi.apiary-mock.com/districts/115/schools/43/menus/"]; 
     checkData =[[NSMutableArray alloc]init]; 
     URLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[Url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 
     NSURLResponse *response = nil; 
     NSError *error = nil; 
     NSData *data = [NSURLConnection sendSynchronousRequest:URLRequest returningResponse:&response error:&error]; 
     NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     SBJsonParser* parser = [[SBJsonParser alloc] init]; 

     NSError *jsonError; 
     if(jsonError == nil) 
     { 
      id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError]; 


      NSLog(@"%@",json); 

      NSDictionary *data = [json objectForKey:@"data"];    
    } 
}); 
+0

後做好這一點你似乎已經完成了解析。我期望的是,你所堅持的是導航它產生的對象的層次結構。 – trojanfoe

+0

我不明白你的問題,你能更清楚你解析/遍歷這個json對象時面臨的問題嗎? –

+0

究竟@trojanfoe我被困在導航對象的層次結構 –

回答

0

如果你的問題是,ÿ OU不知道數據的確切結構,你的JSON,您可以使用類似:

if([json isKindOfClass:[NSDictionnary class]) { 
    // actions for a dictionnary 
    id branch = json[@"your key"]; 
} else if([json isKindOfClass:[NSArray class]) { 
    // actions for an array 
    id branch = json[index]; 
} 

還行if(jsonError == nil)沒有任何意義,你做前人的精力[NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];