好吧,我很困惑。所以,我有一個由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