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?