好吧,我很困惑。所以,我有一个由JSON字符串,它看起来像填充一个NSDictionary:Objective-C for Dummies:如何通过NSDictionary内部的NSDictionary进行循环?
{"Success":true,"Devices":[{"UDId":"...","User":"...","Latitude":0.0,"Longitude":0.0}]}
现在,我知道如何检查Success
是true
,但我通过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
}
}
}
我真的在欣赏一些帮助这个。我似乎无法将我的头围绕它...
在此先感谢!
谢谢!我还学习了一些额外的init代码。 – Gup3rSuR4c 2011-02-09 01:16:04