2011-02-08 77 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