我在Objective C中开发了一个IOS应用程序,我在其中调用URL来检索数组JSON对象(餐馆)。我想将它们解析为Objective-C 模型。使用它们填充到我已经设计的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并检索和打印解析 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);
}
}];
如何创建餐厅数据模型。或者还有其他方法可以解决这个问题吗?
答案 0 :(得分:4)
不需要任何外部库。查看您获得的数据,很明显您希望每个类都代表一个联系人,一个位置,一个餐馆,并且您希望将JSON响应转换为一系列餐馆。很明显,联系人,位置和餐馆应该拥有哪些属性。
对于每个类,您根本不需要init方法,因此声明一个带有标志的init方法,使其使用非法。对于每个类,声明一个方法initWithJSONDictionary:(NSDictionary*)dict
,它将相应的字典作为参数,从字典中提取所有属性,记录值是否不是您期望的值,如果数据有问题则返回nil。
使用NSJSONSerialization
解析完整的JSON数据,检查它是否为数组,每个数组元素都是字典,并为每个字典调用[[MyRestaurant alloc] initWithJSONDictionary:...]
。
这绝对是直截了当的,对于像你的情况那样简单的东西,你应该花一两个小时为此编写防弹代码。
答案 1 :(得分:3)
你可以从这里使用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 :(得分:2)
使用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文件
@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];
答案 3 :(得分:1)
将json中的餐厅数组存储在模型对象中,然后您可以从模型中检索集合视图所需的数据,以便所有解析内容都保留在模型中。
.h
@interface Model : NSObject
@property(nonatomic,strong)NSArray *restaurants;
@end
.m
@implementation Model
-(NSIteger)getRestaurantsCount{
return restaurants.count;
}
//getRestaurantAtIndex:
//getRestaurantNameAtIndex:
@end
答案 4 :(得分:0)
您可以将回调中获得的数据解析为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"];
答案 5 :(得分:0)
查看此repo,它还带有简单的coredata集成 https://github.com/BadChoice/daikiri