Parse a JSON string into an array of Objective-C objects

I have a JSON string returned from a web rest service request, I want to parse this string in an array of objects from a specific class, this is a JSON string

[ { "validationCode": null, "FirstName": "Samer", "LastName": "Shame", "MobileNumber": "0991992993", "SimNumber": null, "Email": null, "PhoneNumber": "0991992994", "Name": "Abo Alshamat", "ID": 1 }, { "validationCode": null, "FirstName": "Ahmad", "LastName": "Ali", "MobileNumber": "0992993994", "SimNumber": null, "Email": null, "PhoneNumber": "0992993995", "Name": "AL-Kamal", "ID": 2 }, { "validationCode": null, "FirstName": null, "LastName": null, "MobileNumber": "0993377800", "SimNumber": null, "Email": null, "PhoneNumber": null, "Name": "Abo-MAhmoud", "ID": 12 }, { "validationCode": null, "FirstName": "William", "LastName": "Ammar", "MobileNumber": "0993994995", "SimNumber": null, "Email": null, "PhoneNumber": "0993994996", "Name": "Four Season", "ID": 3 }, { "validationCode": null, "FirstName": "Ammar", "LastName": "William", "MobileNumber": "0999555777", "SimNumber": null, "Email": null, "PhoneNumber": null, "Name": "uuuuu", "ID": 20 }, { "validationCode": null, "FirstName": null, "LastName": null, "MobileNumber": "0999888777", "SimNumber": null, "Email": null, "PhoneNumber": null, "Name": "NewOneFromI2", "ID": 18 }, { "validationCode": null, "FirstName": null, "LastName": null, "MobileNumber": "0999998997", "SimNumber": null, "Email": null, "PhoneNumber": "0999999998", "Name": "JOURY", "ID": 4 }, { "validationCode": null, "FirstName": null, "LastName": null, "MobileNumber": "202020", "SimNumber": null, "Email": null, "PhoneNumber": null, "Name": "TestTestRestaurant,Ammar,Hamed", "ID": 19 } ] 

class I want to get instances from:

 @interface Restaurant : NSObject @property (nonatomic,strong) NSString *ID; @property (nonatomic,strong) NSString* FirstName; @property (nonatomic,strong) NSString* LastName; @property (nonatomic,strong) NSString* MobileNumber; @property (nonatomic,strong) NSString* simNumber; @property (nonatomic,strong) NSString* PhoneNumber; @property (nonatomic,strong) NSString* Name; @end 

What is the best way to do this, excuse me, maybe the question is related to basic knowledge, but I'm new to objective C

Thank you for your time.

+7
json ios objective-c ios5
source share
2 answers

I would suggest implementing the init method for your Restaurant class.

 -(instancetype) initWithParameters:(NSDictionary*)parameters { self = [super init]; if (self) { //initializations _validationCode = parameters[@"validationCode"]; // may be NSNull _firstName = [parameters[@"FirstName"] isKindOfClass:[NSNull class]] ? @"" : parameters[@"FirstName"]; ... } return self; } 

Note: the fact that you have JSON Nulls makes your initialization a bit complicated. You need to decide how you want to initialize the property when the corresponding JSON value is Null.

Your parameters dictionary will be the first dictionary from the JSON array that you received from the server.

First create a JSON view that is an NSArray object from JSON:

 NSError* localError; id restaurantsObjects = [NSJSONSerialization JSONObjectWithData:data options:0 error:&localError]; 

IFF didn't fail; your restaurantsObjects should now be an NSArray object containing restaurants like NSDictionary s.

Now you will need to create an NSMutableArray that will be populated with Restaurant objects:

 NSMutableArray* restaurants = [[NSMutableArray alloc] init]; for (NSDictionary* restaurantParameters in restaurantsObjects) { Restaurant* restaurant = [Restaurant alloc] initWithParameters: restaurantParameters]; [restaurants addObject:restaurant]; } 

and finally, you can set the restaurants property in some controller:

 self.restaurants = [restaurants copy]; 
+7
source share

There is an dictation array in your JSON ... First, convert the data to NSArray.

 NSError *jsonError = nil; NSArray *jsonArray = (NSArray *)[NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&jsonError]; 

You now have an array of JSON iteration dictionaries.

  for (NSDictionary *dic in jsonArray){ // Now you have dictionary get value for key NSString *firstName = (NSString*) [dic valueForKey:@"FirstName"];//We are casting to NSString because we know it will return a string. do this for every property... } 
+6
source share

All Articles