【发布时间】:2015-11-19 23:19:12
【问题描述】:
我正在用 Objective C 开发一个 IOS 应用程序,我将在其中调用一个 URL 来检索数组 JSON 对象(餐厅)。我想将它们解析为一个objective-C Model。使用它们填充到我已经设计的 UICollectionView 上。我的任务要求我设计这个模型来存储 Json 对象,然后使用它们填充到 UICollectionView。我不知道如何在 Objective-C 中实现这一点,请帮助我。检索到的JSON如下。
"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"
]
}
}
]
以下代码展示了如何调用 URL 并检索和打印 parse JSON 对象。
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);
}
}];
如何创建餐厅数据模型。或者还有其他方法可以解决这个问题吗?
【问题讨论】:
-
Objective-C 模型是什么意思?你是说普通班吗?还是您想使用 Core Data 来存储它?
-
抱歉含糊不清,我是说普通班。
标签: ios objective-c json uicollectionview