在iOS中,我如何parsing一个JSONstring到一个对象中

我来自Android的开发,所以很抱歉,如果我在这里错过了明显的iOS概念。

我有一个JSON提要,看起来像:

{"directory":[{"id":0,"fName":"...","lName":"...","title":"...","dept":"...","bld":"...","room":"...","email":"...","phone":"..."},{"id":1,"fName":"...","lName":"...","title":"...","dept":"...","bld":"...","room":"...","email":"...","phone":"..."}]}

然后,我有一个Staff.h和.m与类与属性匹配它(id,fName,lName)等。

我一直在这工作了几个小时,但我似乎无法parsingJSONstring的一个Staff对象的数组。 最终目标是让他们进入核心数据,所以任何build议都会很好。

我读过的教程没有显示如何使用{“directory”forms的JSONstring:[{…}]}在我的Android应用程序中执行此操作时没有任何问题,但是我已经退出在objective-c中的iOS(6)的想法。

谢谢阅读。

你可以这样做

 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];//response object is your response from server as NSData if ([json isKindOfClass:[NSDictionary class]]){ //Added instrospection as suggested in comment. NSArray *yourStaffDictionaryArray = json[@"directory"]; if ([yourStaffDictionaryArray isKindOfClass:[NSArray class]]){//Added instrospection as suggested in comment. for (NSDictionary *dictionary in yourStaffDictionaryArray) { Staff *staff = [[Staff alloc] init]; staff.id = [[dictionary objectForKey:@"id"] integerValue]; staff.fname = [dictionary objectForKey:@"fName"]; staff.lname = [dictionary objectForKey:@"lName"]; //Do this for all property [yourArray addObject:staff]; } } } 

使用: NSJSONSerialization

您可以使用NSJSONSerialization类将JSON转换为Foundation对象,并将Foundation对象转换为JSON。

 An object that may be converted to JSON must have the following properties: The top level object is an NSArray or NSDictionary. All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull. All dictionary keys are instances of NSString. Numbers are not NaN or infinity. 

你会得到NSDictionary然后你可以parsing(创build)到你的对象,然后在CoreData中使用它。

使用以下代码:

 NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

这将把json数据转换成NSDictionary,类似于android上的hashmap。 我认为这会帮助你。 🙂

你可以使用NSJSonSerialisation或AFNetworking库。 这是AFNetworkingparsingjson响应的例子

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); NSDictionary *json = (NSDictionary *)responseObject; NSArray *staffArray = json[@"directory"]; [staffArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){ Staff *staff = [[Staff alloc] init]; staff.id = [[obj objectForKey:@"id"] integerValue]; staff.fname = [obj objectForKey:@"fName"]; staff.lname = [obj objectForKey:@"lName"]; //add data to new array to store details [detailsArray addObect:staff); } ]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; 

然后使用Core Data framework来存储数据。

我会看看RestKit 。 它提供了对象映射和CoreData支持的存储。

为此,您可以使用SBJSON框架。

您必须将响应string转换为类似的NSDictionary

  NSString *responseString = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; NSDictionary *dic = [responseString JSONValue]; 

现在你可以为Staff类创build一个对象。

 Staff *staff = [[Staff alloc]init]; 

然后你可以像这样在这个对象中存储值

 staff.firstname = [[[dic objectForKey:@"directory"] objectAtIndex:0] objectForKey:@"fName"]; 

现在,您可以将这个单个对象传递给其他类

你可以使用@ janak-nirmal提出的手工解决scheme,或者使用像jastor, https: //github.com/elado/jastor这样的库,它没有多大区别。 我提醒你们注意Restkit,因为在我看来,效益vs痛苦的比例很低。 而且,在你的情况下,它可以像使用坦克一样杀死苍蝇。