1

I manage to get Json for my server and parse in Xcode , I can then convert Json to a object and loaded my uitableview, the problems is my Json have 7 objects ..

you can see the full Json here

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";
                };
            }, ......

with this code I can get friday's "Friday" and display the GymClass info "GymClass" on my table

- (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;
}

I can get the elements for one day but how do I get the Elements for every day (Mon,Tue...Sun)so i can display on my table by sections?

thanks for your help..

1 Answer 1

8

Well in your code, you already are doing that:

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

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

Wen can start from there to retrieve only the object 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);
}

Here, i am using Fast Enumeration to iterate over the dictionary, you should check this here.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi I know how to get info for one Day "Friday" , i am just don't know how to get the info for all the days ..
@HernandoZ Take a look the second piece of code. There i'm not using Friday, only iterate over all
great thanks , so if I used Fast Enumeration what do i need to do ? save each day to NSArray ? then I would have 7 arrays to collect information from ? , sorry to be a pain can you explain what process I should follow to get all the days info in one table sorted by Day section, I know how to do it for one array which i did for "Friday" , but using multiple Array ? Thanks for your help ..
@HernandoZ You can use only the array posts as data source of a table view. The number of sections is the size of the array and the number of rows of each sections is the size of the n-array.
@ Jonathan Nuñez Aguin one more question , how do i divide the array posts in day sections? thanks for your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.