将JSON字符串解析为对象数组Objective C [英] parse JSON string into array of objects Objective C
问题描述
我有一个从休息Web服务请求返回的JSON字符串,
我想将此字符串解析为确定类的对象数组,
这是JSON字符串
I have a JSON String returned from rest web service request, I want to parse this string into Array of objects from determined class, this is the JSON string
[
{
"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
}
]
我要从中获取实例的类是:
the class I want to get instances from is:
@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
what is the best way to do that,excuse me maybe the question is from basic Knowledge but I am new to objective C
谢谢您的时间。
推荐答案
我建议为 Restaurant
类实现一个init方法。
I would suggest to implement an init method for your Restaurant
class.
-(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时,您需要决定如何初始化属性。
Note: the fact that you may have JSON Nulls, makes your initialization a bit elaborate. You need to decide how you want to initialize a property, when the corresponding JSON value is Null.
您的参数
dictionary将是来自服务器的JSON数组的第一级字典。
Your parameters
dictionary will be the first level dictionary from the JSON Array which you got from the server.
首先,创建一个JSON表示,即JSON中的NSArray对象: / p>
First, create a JSON representation, that is a NSArray object from the JSON:
NSError* localError;
id restaurantsObjects = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&localError];
IFF这没有失败,你的 restaurantsObjects
现在应该是 NSArray
对象,其中包含餐馆 NSDictionary
s。
IFF this did not fail, your restaurantsObjects
should now be an NSArray
object containing the restaurants as NSDictionary
s.
现在,可以直接创建一个 NSMutableArray
,它将填充 Restaurant
对象:
Now, it will be straight forward to create a NSMutableArray
which will be populated with Restaurant
objects:
NSMutableArray* restaurants = [[NSMutableArray alloc] init];
for (NSDictionary* restaurantParameters in restaurantsObjects) {
Restaurant* restaurant = [Restaurant alloc] initWithParameters: restaurantParameters];
[restaurants addObject:restaurant];
}
最后,您可以设置一个属性餐馆
在某些控制器中:
and finally, you may set a property restaurants
in some controller:
self.restaurants = [restaurants copy];
这篇关于将JSON字符串解析为对象数组Objective C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!