2017-06-03 27 views
-3
{ 
"team": 
{ 
"players": 
{ 
    "1": 
    { 
    "teamName":"Royal Challenge Bangalore", 
    "shortName":"RCB", 
    "11":{ 
    "name":"Virat Kholi", 
    "Iscaptain":true, 
    "postion":"2", 
    "runs":"6000" 
     }, 
    "12":{ 
    "name":"Chris Gyale", 
    "postion":"1", 
    "runs":"4000" 
     }, 
    "13":{ 
    "name":"AB", 
    "postion":"4", 
    "runs":"5000" 
     } 
    }, 
    "2": 
    { 
    "teamName":"Kolkatta Knight Riders", 
    "shortName":"KKR", 
    "11":{ 
    "name":"Robin Uttapa", 
    "postion":"1", 
    "runs":"6000" 
     }, 
    "12":{ 
    "name":"Sunil Narayan", 
    "postion":"2", 
    "runs":"4000" 
     }, 
    "13":{ 
    "name":"Gautam Ganmbhir", 
    "Iscaptain":true, 
    "postion":"4", 
    "runs":"5000" 

     }  
    } 
    } 
} 
}`enter code here` 

回答

0

你可以這樣做。 captains將包含NSDictionary對象與相關數據

NSString *json = @"your json here..."; 
    NSError *error; 
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] 
                 options:0 
                  error:&error]; 

    if (error) { 
     // TODO: handle error... 
    } 

    NSArray *teams = [[[dict objectForKey:@"team"] objectForKey:@"players"] allObjects]; 
    NSMutableArray *captains = [NSMutableArray new]; 

    for (NSDictionary *team in teams) { 
     for (id item in [team allValues]) { 
      if ([item isKindOfClass:[NSDictionary class]]) { 
       if ([(NSDictionary *) item objectForKey:@"Iscaptain"]) { 
        [captains addObject:item]; 
       } 
      } 
     } 
    } 
+0

非常感謝你......這對我的工作.. –

相關問題