Xcode如何解析Json Objects

Her*_*doZ 1 xcode json uitableview ios5

我设法让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";
                };
            }, ......
Run Code Online (Sandbox Code Playgroud)

使用此代码,我可以获得星期五的"星期五",并在我的桌子上显示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;
}
Run Code Online (Sandbox Code Playgroud)

我可以获得一天的元素但是如何每天都获得元素(星期一,星期二......太阳),所以我可以按照部分显示在我的桌子上?

谢谢你的帮助..

Jon*_*uin 8

在您的代码中,您已经这样做了:

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

NSLog(@"array : %@",dictionary);
NSArray *array = [[dictionary objectForKey:@"posts"] objectForKey:@"Friday"]; // retrieve that Day Gym Classes
Run Code Online (Sandbox Code Playgroud)

Wen可以从那里开始只检索对象posts:

NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSDictionary *posts = [dictionary objectForKey:@"posts"];

//Iterate over posts

for (NSArray *aDay in posts){
     //Do something 
     NSLog(@"Array: %@", aDay);
}
Run Code Online (Sandbox Code Playgroud)

在这里,我使用快速枚举来迭代字典,你应该在这里检查.