将JSON字符串解析为对象数组Objective C

时间:2013-11-13 13:07:31

标签: ios objective-c json ios5

我有一个从休息Web服务请求返回的JSON字符串, 我想将这个字符串解析为确定类的对象数组, 这是JSON字符串

[
    {
        "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
    }
]

我想从中获取实例的类是:

@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

最好的方法是什么,请原谅我的问题可能来自基础知识,但我是新客观的C

谢谢你的时间。

2 个答案:

答案 0 :(得分:8)

我建议为Restaurant类实现一个init方法。

-(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;
}

注意:您可能拥有JSON Null,这使您的初始化有点复杂。当相应的JSON值为Null时,您需要决定如何初始化属性。

您的parameters字典将是您从服务器获取的JSON数组中的第一级字典。

首先,创建一个JSON表示,即JSON中的NSArray对象:

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

IFF没有失败,您的restaurantsObjects现在应该是一个NSArray对象,其中包含餐馆NSDictionary s。

现在,可以直接创建NSMutableArray,其中将填充Restaurant个对象:

NSMutableArray* restaurants = [[NSMutableArray alloc] init];
for (NSDictionary* restaurantParameters in restaurantsObjects) {
    Restaurant* restaurant = [Restaurant alloc] initWithParameters: restaurantParameters];
    [restaurants addObject:restaurant];
}

最后,您可以在某个控制器中设置属性restaurants

self.restaurants = [restaurants copy];

答案 1 :(得分:6)

你的JSON有一系列词典...... 首先将您的数据转换为NSArray。

NSError *jsonError = nil;

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

现在你有一系列JSON字典迭代。

  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...  
    }