Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JSON String returned from rest web service request, I want to parse this string into Array of objects from determined class, this is the 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
    }
]

the class I want to get instances from is:

@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 that,excuse me maybe the question is from basic Knowledge but I am new to objective C

thank you for your time.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

I would suggest to implement an 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 may have JSON Nulls, makes your initialization a bit elaborate. You need to decide how you want to initialize a property, when the corresponding JSON value is Null.

Your parameters dictionary will be the first level dictionary from the JSON Array which you got from the server.

First, create a JSON representation, that is a NSArray object from the JSON:

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

IFF this did not fail, your restaurantsObjects should now be an NSArray object containing the restaurants as NSDictionarys.

Now, it will be straight forward to create a NSMutableArray which 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 may set a property restaurants in some controller:

self.restaurants = [restaurants copy];
share|improve this answer
add comment

Your JSON have An array of dictionaies... First convert your data to NSArray. NSError *jsonError = nil;

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

Now you have array of JSON dictionaries iterate.

  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...  
    }
share|improve this answer
 
One thing to be aware of is that firstName could be equal to [NSNull null] if 'FirstName' is 'null' in the JSON. You actually don't know what it'll return (it could be a NSString, it could be an NSDictionary!), so extra care might be necessary, if you're not confident of how exactly the JSON will be formed (if you control the server side, it's less of a concern). –  Kitsune Nov 13 '13 at 13:26
 
In this case we know that FirstName is string... So we are using like this other wise we can use NSObject... And if there is no FirstName... it will return nil... –  C_X Nov 13 '13 at 13:33
 
Many of the FirstName in the example JSON are in fact null, which means [NSNull null] when deserialized. If FirstName doesn't appear at all, you would get nil when deserialized. If you're not careful, and the JSON is formatted differently than you expect/assume, you could end up with crashes (likely invalid selector). –  Kitsune Nov 13 '13 at 13:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.