[英]parse JSON string into array of objects Objective C
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 我有一个从休息Web服务请求返回的JSON字符串,我想将此字符串解析为确定类的对象Array,这是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
}
]
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
what is the best way to do that,excuse me maybe the question is from basic Knowledge but I am new to objective C 什么是最好的方法,对不起,也许问题来自基础知识,但我是客观C的新手
thank you for your time. 感谢您的时间。
I would suggest to implement an init method for your Restaurant
class. 我建议为你的
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;
}
Note: the fact that you may have JSON Nulls, makes your initialization a bit elaborate. 注意:你可能有JSON Null的事实,使你的初始化有点复杂。 You need to decide how you want to initialize a property, when the corresponding JSON value is Null.
当相应的JSON值为Null时,您需要决定如何初始化属性。
Your parameters
dictionary will be the first level dictionary from the JSON Array which you got from the server. 您的
parameters
字典将是您从服务器获得的JSON数组中的第一级字典。
First, create a JSON representation, that is a NSArray object from the JSON: 首先,创建一个JSON表示,即JSON中的NSArray对象:
NSError* localError;
id restaurantsObjects = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&localError];
IFF this did not fail, your restaurantsObjects
should now be an NSArray
object containing the restaurants as NSDictionary
s. IFF没有失败,你的
restaurantsObjects
现在应该是一个NSArray
对象,包含餐馆作为NSDictionary
s。
Now, it will be straight forward to create a NSMutableArray
which will be populated with Restaurant
objects: 现在,创建一个将使用
Restaurant
对象填充的NSMutableArray
将是直截了当的:
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: 最后,您可以在某个控制器中设置一个属性
restaurants
:
self.restaurants = [restaurants copy];
Your JSON have An array of dictionaies... First convert your data to NSArray. 你的JSON有一系列的字典...首先将你的数据转换为NSArray。
NSError *jsonError = nil;
NSArray *jsonArray = (NSArray *)[NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&jsonError];
Now you have array of JSON dictionaries iterate. 现在你有一系列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...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.