2012-06-20 143 views
1

我正在解析的JSON可以在這裏找到'redacted'。Objective-C/JSON,字典數組,解析嵌套在字典中的數組

我可以正確地從這個JSON中獲取除通知和設備以外的所有對象。 我有很多麻煩能夠獲取包含deviceId的設備字典數組。我的代碼現在看起來像這樣

dispatch_async(kBgQueue, ^{ 
    NSData* data = [NSData dataWithContentsOfURL: 
        iViuListOfCustomersURL]; 

    [self performSelectorOnMainThread:@selector(fetchedData:) 
          withObject:data waitUntilDone:NO]; 
}); 




- (void) fetchedData:(NSData *)responseData { 
NSError* error; 
NSDictionary* json; 
    json = [NSJSONSerialization 
          JSONObjectWithData:responseData // 1 

          options:kNilOptions 
          error:&error]; 

    NSArray* customers = json; 


    for(int i = 0; i < customers.count ; i++){ 
     NSDictionary *customer = [customers objectAtIndex:i]; 
     cmsServer = [customer objectForKey:@"cmsServer"]; 
     iconUrl = [NSURL URLWithString:[customer objectForKey:@"iconUrl"]]; 
     wideLogo = [customer objectForKey:@"wideLogo"]; 
     customerNo = [NSNumber numberWithInt:[[customer objectForKey:@"customerNo"] intValue]]; 
     placeName = [customer objectForKey:@"placeName"]; 
     distance = [NSNumber numberWithFloat:[[customer objectForKey:@"distance"] floatValue]]; 
     placeId = [NSNumber numberWithInt:[[customer objectForKey:@"placeId"] intValue]]; 
     address = [customer objectForKey:@"address"]; 
     cmsName = [customer objectForKey:@"cmsName"]; 
     hasLocations = [NSNumber numberWithInt:[[customer objectForKey:@"hasLocations"]intValue]]; 
     isFavorite = [NSNumber numberWithInt:[[customer objectForKey:@"isFavorite"] intValue]]; 
     longitude = [NSNumber numberWithFloat:[[customer objectForKey:@"longitude"] floatValue]]; 
     latitude = [NSNumber numberWithFloat:[[customer objectForKey:@"latitude"] floatValue]]; 
     hasArtifacts = [NSNumber numberWithInt:[[customer objectForKey:@"hasArtifacts"]intValue]]; 

     // I have tried a number of things and this was what I had for the last try. 
     devices = [NSArray arrayWithObject:[customer objectForKey:@"devices"]]; 
     NSLog(@"%@",devices.count); 
    } 

}

我是比較新的Objective-C和想弄清楚這一點對我自己,但我花了在這個問題上過多的時間,我無法繼續沒有這些設備ID。

謝謝!

回答

1

我不知道它是否會有所作爲,但你嘗試過:

devices = [NSArray arrayWithArray:[customer objectForKey:@"devices"]]; 

你可以嘗試正在經歷它的元素,加入逐一進入設備的另一件事:

for(DeviceObject *deviceObject in [customer objectForKey:@"devices"]) 
{ 
    [devices addObject:deviceObject]; 
} 

你可以看到的另一件事是你有時在你的json中傳遞的空值。

+0

yesssssssssss!謝謝!第二個選項奏效! :) – Garfbargle

+0

您可以將此答案標記爲正確的。 –