2011-02-08 80 views
1

好吧,我很困惑。所以,我有一個由JSON字符串,它看起來像填充一個NSDictionary:Objective-C for Dummies:如何通過NSDictionary內部的NSDictionary進行循環?

{"Success":true,"Devices":[{"UDId":"...","User":"...","Latitude":0.0,"Longitude":0.0}]} 

現在,我知道如何檢查Successtrue,但我通過Devices(JSON對象)的陣列需要循環並創建一個Devices(內部應用程序對象)的內部數組,我不知道該怎麼做。有人可以解釋如何做到這一點?

這裏是我的Device.m/h

#import <CoreLocation/CoreLocation.h> 
#import <Foundation/Foundation.h> 

@interface Device : NSObject { 
    NSString *udId; 
    NSString *name; 
    NSNumber *latitude; 
    NSNumber *longitude; 
} 

@property (nonatomic, retain) NSString *udId; 
@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSNumber *latitude; 
@property (nonatomic, retain) NSNumber *longitude; 

#pragma mark - 
#pragma mark MKAnnotation Properties 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

@end 

---- 

#import "Device.h" 

@implementation Device 

@synthesize udId, name, latitude, longitude; 

- (CLLocationCoordinate2D)coordinate { 
    CLLocationCoordinate2D internalCoordinate; 

    internalCoordinate.latitude = [self.latitude doubleValue]; 
    internalCoordinate.longitude = [self.longitude doubleValue]; 

    return internalCoordinate; 
} 

- (void)dealloc { 
    [udId release]; 
    udId = nil; 

    [name release]; 
    name = nil; 

    [latitude release]; 
    latitude = nil; 

    [longitude release]; 
    longitude = nil; 

    [super dealloc]; 
} 

@end 

這裏的地方我應該讀的響應,並且將其轉換爲對象的方法,我可以使用:

- (void)requestFinished:(ASIHTTPRequest *)request { 
    if (![request error]) { 
     NSError *jsonError = nil; 
     NSDictionary *jsonDictionary = [NSDictionary dictionaryWithJSONString:[request responseString] error:&jsonError]; 

     if (!jsonError || ([[jsonDictionary objectForKey:@"Success"] intValue] == 1)) { 
      // READ "DEVICES" AND CONVERT TO OBJECTS 
     } else { 
      // AUTHORIZATION FAILED 
     } 
    } 
} 

我真的在欣賞一些幫助這個。我似乎無法將我的頭圍繞它...

在此先感謝!

回答

1

首先,您需要爲Device類定義初始化程序/構造函數。

device.h中

- (id)initWithUdid:(NSString *)udid name:(NSString *)name latitude:(NSNumber *)lat longitude:(NSNumber *)lon; 

設備。米

- (id)initWithUdid:(NSString *)udid name:(NSString *)name latitude:(NSNumber *)lat longitude:(NSNumber *)lon { 
    if (self = [super init]) { 
     self.udid = udid; 
     self.name = name; 
     self.latitude = lat; 
     self.longitude = lon; 
    } 
    return self; 
} 

然後你就可以初始化一個新的對象,如:

Device *dev = [[Device alloc] initWithUdid:@"a udid" name:@"the name" latitude:latNum longitude:lonNum]; 

所以,你應該能夠遍歷數組,並建立自己的設備對象,像這樣:

NSArray *devicesArray = [dict objectForKey:@"Devices"]; 
for (NSDictionary *d in devicesArray) { 
    Device *dev = [[Device alloc] initWithUdid:[d objectForKey:@"UDId"] 
             name:[d objectForKey:@"User"] 
            latitude:[NSNumber numberWithDouble:[d objectForKey:@"Latitude"]] 
            longitude:[NSNumber numberWithDouble:[d objectForKey:@"Latitude"]]]; 
} 
+0

謝謝!我還學習了一些額外的init代碼。 – Gup3rSuR4c 2011-02-09 01:16:04

0

您想從頂級字典訪問設備字典數組,就像您執行成功值一樣。然後遍歷字典你可以使用每個的-keyEnumerator方法迭代其密鑰。

- (void)requestFinished:(ASIHTTPRequest *)request { 
    if (![request error]) { 
     NSError *jsonError = nil; 
     NSDictionary *jsonDictionary = [NSDictionary dictionaryWithJSONString:[request responseString] error:&jsonError]; 

     if (!jsonError || ([[jsonDictionary objectForKey:@"Success"] intValue] == 1)) { 
      NSArray* deviceArray = [jsonDictionary objectForKey:@"Devices"]; 
      for(NSDictionary* dict in deviceArray) 
      { 
       for(NSString* key in [dict keyEnumerator]) 
       { 
        NSLog(@"%@ -> %@", key, [dict objectForKey:key]); 
       } 
      } 
      // READ "DEVICES" AND CONVERT TO OBJECTS 
     } else { 
      // AUTHORIZATION FAILED 
     } 
    } 
} 
3

你快到了。在你的代碼,你說:

// READ "DEVICES" AND CONVERT TO OBJECTS 

做到這一點:

NSArray * devices = [jsonDictionary objectForKey:@"Devices"]; 
    for(NSDictionary * deviceInfo in devices) { 
    Device * d = [[[Device alloc] init] autorelease]; 
    [d setLatitude:[deviceInfo objectForKey:@"Latitude"]]; 
    [d setLongitude:[deviceInfo objectForKey:@"Longitude"]]; 
    [d setName:[deviceInfo objectForKey:@"User"]]; 
    [d setUdId:[deviceInfo objectForKey:@"UDId"]]; 
    // do some stuff with d 
    } 

這是怎麼回事:我沒有看到你正在使用轉換什麼JSON庫,但假設它就像TouchJSON或SBJSON,JSON數組自動變成一個NSArray實例,而NSArray的內部哈希是NSDictionary對象。在你已經反序列化了這個JSON字符串的時候,你處理的所有事情都將是NSString,NSNumber,NSArray和NSDictionary的實例(並且取決於庫,NSNull表示空值)。

0

聽起來像是你需要重複使用您的線路:在

[jsonDictionary objectForKey:@"Devices"] 

你真的需要弄清楚什麼類型分別返回在看看

[jsonDictionary objectForKey:@"Success"] 

嘗試。 如果幸運的話,它會返回一個NSDictionary,或者您可以輕鬆地將其轉換爲NSDictionary