2012-01-17 72 views
2

我正在從JSON Web服務JSON陣列選擇器視圖iPhone

List ARRAY: (
    { 
    assets =   (
        { 
      identity = 34DL3611; 
      systemId = 544507; 
     }, 
        { 
      identity = 34GF0512; 
      systemId = 5290211; 
     }, 
        { 
      identity = 34HH1734; 
      systemId = 111463609; 
     }, 
        { 
      identity = 34HH1736; 
      systemId = 111463622; 
     }, 
        { 
      identity = 34YCJ15; 
      systemId = 294151155; 
     } 
    ); 
    identity = DEMO; 
    systemId = 4921244; 
}) 

該數據通過使用此代碼:

NSArray *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSLog(@"Response data: %@", responseString); 

NSLog(@"List ARRAY: %@", list); 

NSDictionary *dict = [list objectAtIndex: 0]; 

NSMutableArray *vehicleGroups = [dict objectForKey:@"identity"]; 

NSLog(@"Vehicle Groups: %@", vehicleGroups); 

這裏是我使用的選擇器代碼:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
return 1;} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component { 
return [vehicleGroups count];} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{return nil;} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 
} 

應用程序崩潰在線

return [vehicleGroups count]; 

委託方法numberOfRowsInComponentpickerView。我不明白 - 爲什麼我面臨這個問題?

//代碼////

NSArray *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSLog(@"Response data: %@", responseString); 

NSLog(@"List ARRAY: %@", list); 

NSDictionary *dict = [list objectAtIndex: 0]; 

vehicleList = [dict objectForKey: @"assets"]; 

self.vehicleGroups = [[NSMutableArray alloc] init]; 

vehicleGroups = [dict objectForKey:@"identity"]; 

NSLog(@"Vehicle Groups: %@", vehicleGroups); 

NSString *identity = [dict objectForKey: @"identity"]; 

NSString *systemid = [dict objectForKey:@"systemId"]; 

self.listVehicles = [[NSMutableArray alloc] init]; 

self.listVehiclesID =[[NSMutableArray alloc]init]; 

NSLog(@"GroupOfVehicles: %@", groupOfVehicles); 

for (NSUInteger index = 0; index < [vehicleList count]; index++) { 

    itemDict = [vehicleList objectAtIndex: index]; 

    [self.listVehicles addObject:[itemDict objectForKey:@"identity"]]; 

    [self.listVehiclesID addObject:[itemDict objectForKey:@"systemId"]]; 
} 

NSLog(@"Group Name: %@", identity); 

NSLog(@"Assets: %@", listVehicles); 

NSLog(@"Assets System ID: %@", listVehiclesID); 

NSLog(@"GroupSystemID: %@", systemid); 
+0

有沒有什麼記錄,它給了我一個消息:程序接收到的信號:EXC_BAD_ACCESS – 2012-01-17 07:07:39

+0

有什麼可以解決的問題? – 2012-01-17 07:11:04

+0

我收到了:Vehicle Groups:DEMO – 2012-01-17 07:13:29

回答

2

回答我的問題引導人,如果他們面臨同樣的問題,其實我在這裏做以獲取身份所要求的陣列:DEMO如下:

vehicleGroups =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSLog(@"Response data: %@", responseString); 

NSLog(@"List ARRAY: %@", vehicleGroups); 

NSDictionary *dict1 = [vehicleGroups objectAtIndex: 0]; 

NSString *identity1 = [dict objectForKey: @"identity"]; 

NSLog(@"dictNEW: %@", dict1); 

groupOfVehicles = [[NSMutableArray alloc] init]; 
for (NSUInteger index = 0; index < [vehicleGroups count]; index++) { 

    itemDict = [vehicleGroups objectAtIndex: index]; 

    [groupOfVehicles addObject:[itemDict objectForKey:@"identity"]]; 
} 

NSLog(@"Group Name NEW: %@", identity1); 

NSLog(@"Groups NEW: %@", groupOfVehicles); 

使用代碼我已經正確了後陣爲我所需的數據

7

你必須初始化你的陣列的第一

NSDict *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSLog(@"List Dict: %@", list); 

NSMutableArray *temp = list[@"assets"]; 
NSMutableArray *vehicleGroups = [NSMutableArry array]; 
vehicleGroups = temp[0][@"identity"]; 

NSLog(@"Vehicle Groups: %@", vehicleGroups); 
+0

已經初始化它現在,但問題仍然存在 – 2012-01-17 07:34:28

+0

然後NSLog你的字典,並檢查它的顯示或零也tyr首先NSLog你的字典 – Hiren 2012-01-17 07:44:01

+0

是它不是空的,它被顯示,而不是零 – 2012-01-17 07:52:08

0

保留數組

[vehicleGroups retain]vehicleGroups = [dict objectForKey:@"identity"];

+0

我曾經嘗試這樣做,現在得到了在控制檯上此錯誤: - [__ NSCFString計數]:無法識別的選擇發送到實例 – 2012-01-17 07:38:42

0
NSDictionary *dicts = [list objectAtIndex: 0]; 
NSMutableArray *vehicleGroups = [[NSMutableArray alloc] init]; 
for (NSDictionary *dict in dicts) 
{ 
    NSString* identity = [dict objectForKey:@"identity"]; 
    [vehicleGroups addObject:identity]; 
} 
NSLog("count is %d",[vehicleGroups count]); 

有一個嘗試:)

+0

遇到錯誤[__NSCFString ADDOBJECT:]:無法識別的選擇發送到實例 – 2012-01-17 07:56:43

0

您的JSON數據不是JSON

{ 
    "assets" : [ 
     { 
      "identity" : "34DL3611", 
      "systemId" : 544507 
     }, 
     .... 
     { 
      "identity" = "34YCJ15", 
      "systemId" = 294151155 
     } 
    ], 
    "identity" = "DEMO", 
    "systemId" = 4921244 
} 

這是在JSON中描述的數據的可能示例。

0
NSDictionary *dict = [list objectAtIndex: 0]; 

NSMutableArray *vehicleGroups = [dict objectForKey:@"identity"]; 

它看起來對我來說,dict這裏是一個有三個項目的字典:資產,身份和的systenId。在這個層次上,身份的價值只是一個字符串,「DEMO」,但你試圖把它分配給一個可變數組。我想你想這樣的事情,而不是:

NSDictionary *dict = [list objectAtIndex: 0]; 
NSArray *assets = [dict objectForKey:@"assets"]; 
NSArray *vehicleGroups = [assets objectForKey:@"identity"]; 

(沒關係發送-objectForKey:到一個數組中 - 你回到對應於在接收器的每個對象給予重點對象的數組。)