在发布之前我几乎尝试了所有方法(谷歌、Apple Dev Doc 等)。我希望在发布之前我没有错过任何内容..
我已经构建了一个有效的 JSON <-> Web 服务 <-> iPhone 登录。
使用以下代码:
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]autorelease];
NSDictionary *jsonDict = [responseString JSONValue];
NSString *loginResult = [jsonDict valueForKey:@"LoginResult"];
loginResult 是员工的姓名:例如“Henning Wenger”
相应的 JSON 如下所示:
{"LoginResult":"S:00000412"}
现在我有一个更复杂的任务,我无法找到解决方法:
我从网络服务获取以下 JSON:
{"GetTeamPresentStatusResult"[
{"Firstname":"Steffen","Id":"00000456","IsPresent":"true","Lastname":"Polker"},
{"Firstname":"Erich","Id":"00000455","IsPresent":"true","Lastname":"Welter"},
{"Firstname":"Titus","Id":"00000454","IsPresent":"true","Lastname":"Sommerbeck"}, {"Firstname":"Ruediger","Id":"00000453","IsPresent":"true","Lastname":"Oelmann"},{"Firstname":"Heinz","Id":"00000452","IsPresent":"true","Lastname":"Radelfs"},{"Firstname":"Franz","Id":"00000451","IsPresent":"true","Lastname":"Wippermann"},{"Firstname":"Klaus-Dieter","Id":"00000450","IsPresent":"true","Lastname":"Just"},{"Firstname":"Alan","Id":"00000412","IsPresent":"true","Lastname":"Turing"},{"Firstname":"Konrad","Id":"00000138","IsPresent":"true","Lastname":"Zuse"},{"Firstname":"Marius","Id":"00000112","IsPresent":"true","Lastname":"Sandmann"}]}
我处理此问题的代码开头为:
NSDictionary *jsonDict = [responseString JSONValue];
NSArray *teamStatus = [jsonDict objectForKey:@"GetTeamPresentStatusResult"];
[self BuildDataSource: nil :teamStatus];
teamStatus 现在包含 10 个键/值对。
以下是我尝试为表构建数据源的方法:
- (void)BuildDataSource: (id) sender: (NSArray*)teamStatusData{
teamStatusMutableArray = [[NSMutableArray alloc]init];
teamStatusDataSection = [[NSArray alloc]initWithArray:teamStatusData];
teamStatusDictSectionOne = [NSDictionary dictionaryWithObject:teamStatusDataSection forKey:@"OrgUnits"];
[teamStatusMutableArray addObject:teamStatusDictSectionOne];
[tblView reloadData];
}
我无法弄清楚从这里到哪里去获取我需要的数据。
我是否需要不同的数组,其中一个包含名字/值,另一个包含姓氏/值?如果是,我该怎么做?
以下是我迄今为止尝试过的一些行,但没有帮助:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
//First get the dictionary object
NSDictionary *dictionary = [teamStatusMutableArray objectAtIndex:indexPath.section];
NSString *testString = [dictionary valueForKey:@"Firstname"];
NSArray *testarray = [dictionary objectForKey:@"OrgUnits"];
NSString *test = [testarray JSONRepresentation];
test = [test stringByReplacingOccurrencesOfString:@"[" withString:@""];
test = [test stringByReplacingOccurrencesOfString:@"]" withString:@""];
NSDictionary *jsonDict = [test JSONValue];
}
我需要的是一个包含可以绑定(bind)到表的员工姓名的数据源。从那里我想我可以在没有额外帮助的情况下找到自己的路。
我来自 .NET (C#) 世界,也许这让我太困惑了。
非常感谢..
亨宁
如果您需要更多详细信息,请询问!
SPtail 详细信息:
这是 .h 文件:
#import <UIKit/UIKit.h>
#import "RootViewController.h"
#import "EmployeeDetails.h"
@interface OrgUnitAA:NSObject {
NSString *FirstName;
NSString *Id;
NSString *IsPresent;
NSString *LastName;
}
@property (nonatomic, retain) NSString *FirstName;
@property (nonatomic, retain) NSString *Id;
@property (nonatomic, retain) NSString *IsPresent;
@property (nonatomic, retain) NSString *LastName;
@end
@interface TeamStatusView : UIViewController {
UITableView *tblView;
NSMutableArray *teamStatusMutableArray;
NSArray *teamStatusDataSection;
NSDictionary *teamStatusDictSectionOne;
UIViewController *EmployeeDetails;
UIViewController *RootViewController;
NSMutableData *responseData;
}
@property (nonatomic, retain) IBOutlet UITableView *tblView;
@property (nonatomic, retain) NSArray *teamStatusMutableArray;
@property (nonatomic, retain) NSMutableArray *dummyMutableArrayTimeQuota;
@property (nonatomic, retain) NSArray *teamStatusDataSection;
@property (nonatomic, retain) NSDictionary *teamStatusDictSectionOne;
@property (nonatomic, retain) IBOutlet UIViewController *RootViewController;
@property (nonatomic, retain) IBOutlet UIViewController *EmployeeDetails;
- (void)GetTeamStatus: id;
- (void)BuildDataSource: id: (NSArray*)teamStatusData;
@end
最佳答案
嘿,Web 服务返回的数组包含字典,这是显而易见的。
您可以创建一个扩展 NSObject 的类并转换每个字典,而不是为每个属性使用不同的数组,这样您就可以将对象放在数组而不是字典中。在这种情况下:
@interface OrgUnit:NSObject {
NSString *FirstName;
NSNumber *Id;
bool IsPresent;
NSString *LastName;
}
//And all the properties here
之后,您可以使用将字典转换为对象
OrgUnit *unit = [[OrgUnit alloc] init];
[unit setValuesForKeysWithDictionary:dictionary];
PS:对象中的变量名应与字典中的键名相同
关于iphone - 复杂的 JSON 作为 UITableView iOS 的 NSArray 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7258839/