Create Data Model for JSON - IOS / Objective-C

I am developing an IOS application in Objective-C, in which I call the URL to retrieve an array of JSON objects (restaurants). I want to analyze them on an objective-C Model . Using them to populate on a UICollectionView that I already developed. My task requires me to design this model to store Json objects and then use them to populate on a UICollectionView. I do not know how to achieve this in Objective-C, please help me with this. The resulting JSON is as follows.

"restaurants" : [
{
    "name": "Burger Bar",
    "backgroundImageURL": "http://somthing.com/Images/1.png",
    "category" : "Burgers",
        "contact": {
            "phone": "1231231231",
            "formattedPhone": "(123) 123-1231",
            "twitter": "1twitter"
        },
        "location": {
            "address": "5100 Belt Line Road, STE 502",
            "crossStreet": "Dallas North Tollway",
            "lat": 32.950787,
            "lng": -96.821118,
            "postalCode": "75254",
            "cc": "US",
            "city": "Addison",
            "state": "TX",
            "country": "United States",
            "formattedAddress": [
                "5100 Belt Line Road, STE 502 (Dallas North Tollway)",
                "Addison, TX 75254",
                "United States"
            ]
        }
},
{
    "name": "seafood Kitchen",
    "backgroundImageURL": "http://somthing.com/Images/2.png",
    "category": "Seafood",
        "contact": {
            "phone": "3213213213",
            "formattedPhone": "(321) 321-3213",
            "twitter": "2twitter"
        },
        "location": {
            "address": "18349 Dallas Pkwy",
            "crossStreet": "at Frankford Rd.",
            "lat": 32.99908456526653,
            "lng": -96.83018780592823,
            "postalCode": "33331",
            "cc": "US",
            "city": "Dallas",
            "state": "TX",
            "country": "United States",
            "formattedAddress": [
                "18349 Dallas Pkwy (at Frankford Rd.)",
                "Dallas, TX 75287",
                "United States"
            ]
        }
}

]

The code below shows how to call a URL and retrieve and print JSON parsing objects.

NSString *urlString = @"http://somthing.com/Images/collection.json";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           if (!error) {
                               NSError* parseError;
                               id parse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&parseError];  
                               NSLog(@"%@", parse);
                           }
                       }];

How to create a restaurant data model . Or is there another way to do this?

+4
7

. , , , , , , , JSON . , , .

init, init , . initWithJSONDictionary:(NSDictionary*)dict , , , , nil, .

NSJSONSerialization JSON, , , - , [[MyRestaurant alloc] initWithJSONDictionary:...] .

, - , , , .

+4

JSONModel https://github.com/icanzilb/JSONModel

.h

#import "JSONModel.h"




@interface location : JSONModel
{

}

@property (nonatomic,strong) NSString *address;
@property (nonatomic,strong) NSString *crossStreet;
@property (nonatomic,strong) NSString *lat;
@property (nonatomic,strong) NSString *lng;
@property (nonatomic,strong) NSString *postalCode;
@property (nonatomic,strong) NSString *cc;
@property (nonatomic,strong) NSString *city;
@property (nonatomic,strong) NSString *state;
@property (nonatomic,strong) NSString *country;
@property (nonatomic,strong) NSArray *formattedAddress;
@end




@interface contact:JSONModel
{

}
@property (nonatomic,strong) NSString *phone;
@property (nonatomic,strong) NSString *formattedPhone;
@property (nonatomic,strong) NSString *twitter;
@end

@interface Restaurant : JSONModel
@property (nonatomic,strong) location *objectLocation;
@property (nonatomic,strong) contact *objContact;
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *backgroundImageURL;
@property (nonatomic,strong) NSString *category;

@end

.m file

@implementation Restaurant
-(id)init{
 self = [super init];
if (self) {
   _category = @"";
    _name = @"";
    _backgroundImageURL = @"";


 }
 return self;
}
@end
@implementation contact
-(id)init{
self = [super init];
if (self) {
    _phone = @"";
    _formattedPhone = @"";
    _twitter = @"";


}
return self;
}



@end
@implementation location
-(id)init{
self = [super init];
if (self) {
    _address = @"";
    _crossStreet = @"";
    _lat = @"";
    _lng= @"";
    _postalCode = @"";
    _city = @"";
    _state = @"";
    _cc = @"";
    _country = @"";






 }
return self;
}

//objects is the response object
 NSArray* models = [Restaurant arrayOfModelsFromDictionaries: objects];
+2

Json2Model https://github.com/fredlo2010/Json2Model

4 : Restaurants.h Restaurant.h Contact.h Location.h

Contact.h Contact.n

#import <Foundation/Foundation.h>

@interface Contact : NSObject

@property (strong, nonatomic) NSString *twitter;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *formattedPhone;

- (instancetype) initWithTwitter: (NSString *)twitter andPhone: (NSString *)phone andFormattedPhone: (NSString *)formattedPhone;

@end

#import "Contact.h"

@implementation Contact

- (instancetype) initWithTwitter: (NSString *)twitter andPhone: (NSString *)phone andFormattedPhone: (NSString *)formattedPhone {

    self = [super init];

    if (self) {
        self.twitter = twitter;
        self.phone = phone;
        self.formattedPhone = formattedPhone;
    }
    return self
}

@end
+2

JSON, [] . :

 NSArray *restArray= [parse objectForKey:@"restaurants"];

:

for (NSDictionary *restaurant in restArray){
     NSString* name = [restaurant objectForKey:@"name"];
     NSDictionary* location = [restaurant objectForKey:"location"];
    //etc...
}
+1

, , NSDictionary:

NSData *response = someData; // data you are getting in callback
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];
NSArray *restaurants = [json objectForKey:@"restaurants"];
0

json , , , , .

.h 
@interface Model : NSObject

@property(nonatomic,strong)NSArray *restaurants;

@end

.m
@implementation Model

-(NSIteger)getRestaurantsCount{
    return restaurants.count;
}

//getRestaurantAtIndex:
//getRestaurantNameAtIndex:

@end
0

Reserve this repo, it also includes easy core integration https://github.com/BadChoice/daikiri

0
source

All Articles