我设法为我的服务器获取 Json 并在 Xcode 中解析,然后我可以将 Json 转换为对象并加载我的 uitableview,问题是我的 Json 有 7 个对象..
你可以在这里看到完整的 Json
2012-05-05 10:45:02.727 passingdata[63856:fb03] array : {
posts = {
Friday = (
{
GymClass = {
"CLASS-TYPE2" = "";
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Friday;
TIME = "13:00 - 14:30";
};
}
);
Monday = (
{
GymClass = {
"CLASS-TYPE2" = "Fighters Training";
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Muay Thai ";
"DAY_OF_WEEK" = Monday;
TIME = "09:30 - 11:00";
};
}, ......
使用此代码,我可以获得星期五的“星期五”并在我的桌子上显示 GymClass 信息“GymClass”
- (void)fetchedData:(NSData *)responseData { //parse out the json data
searchResults2 = [NSMutableArray arrayWithCapacity:10];
NSError* error;
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"array : %@",dictionary);
NSArray *array = [[dictionary objectForKey:@"posts"] objectForKey:@"Friday"]; // retrieve that Day Gym Classes
if (array == nil) {
NSLog(@"Expected 'posts' array");
return;
}
for (NSDictionary *resultDict in array)
{
SearchResult *searchResult3;
searchResult3 = [self parseTrack:resultDict];
if (searchResult3 != nil) {
[searchResults2 addObject:searchResult3];
}
}
[self.tableView reloadData];
}
- (SearchResult *)parseTrack:(NSDictionary *)dictionary
{
SearchResult *searchResult1 = [[SearchResult alloc] init];
searchResult1.classType= [[dictionary objectForKey:@"GymClass"] objectForKey:@"CLASS_TYPE"];
searchResult1.classLevel= [[dictionary objectForKey:@"GymClass"] objectForKey:@"CLASS_LEVEL"];
NSLog(@"parse track = %@", searchResult1);
return searchResult1;
}
我可以获取一天的元素,但我如何获取每天(周一,周二...周日)的元素,以便我可以按部分显示在我的桌子上?
感谢您的帮助..