0

I am developing an app based on school menus and dishes where I am stuck is in parsing complex/nested json

Sample Json URL : Json Data

So far I have just done this as mentioned in below code using SBJSON:

dispatch_async(dispatch_get_main_queue(),^ {

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    if (networkStatus == NotReachable) {
        [self.view setUserInteractionEnabled:YES];
        [HUD hideUIBlockingIndicator];

    }

    else
    {

        Url =[NSString stringWithFormat:@"http://private-e8e699-yumyummi.apiary-mock.com/districts/115/schools/43/menus/"];
        checkData =[[NSMutableArray alloc]init];
        URLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[Url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        NSURLResponse *response = nil;
        NSError  *error = nil;
        NSData  *data = [NSURLConnection sendSynchronousRequest:URLRequest returningResponse:&response error:&error];
        NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        SBJsonParser* parser = [[SBJsonParser alloc] init];

        NSError *jsonError;
        if(jsonError == nil)
        {
            id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];


            NSLog(@"%@",json);

            NSDictionary *data = [json objectForKey:@"data"];            
    }
});
Maddy Barakz
  • 11
  • 1
  • 7
  • Well you seem to have done the parsing. What you are stuck on, I expect, is navigating the hierarchy of objects it produces. – trojanfoe Feb 27 '16 at 10:51
  • I don't understand your problem, could you be more clear about the issue you are facing in parsing / traversing this json object? – oiledCode Feb 27 '16 at 10:54
  • exactly @trojanfoe i am stuck in navigating the hierarchy of objects – Maddy Barakz Feb 27 '16 at 11:03
  • Well that is fairly easy. If it's an array then it's `array[index]` and if it's a dictionary it's `dict[@"keyname"]`. Just be prepared for non-existent entries (don't make assumptions). – trojanfoe Feb 27 '16 at 11:07
  • @trojanfoe, can u please help me with a sample code? – Maddy Barakz Feb 27 '16 at 11:29
  • You'd need to show (a portion of) the JSON and what you want from it. – trojanfoe Feb 27 '16 at 11:34
  • What particular data you want from the json file? – Rishab Feb 27 '16 at 12:16
  • The scenario is, i will get a week data from server, then i have to display the data as per date provided by the user. I have 2 tabs breaky,lunchy. the data to be displayed are menus, dishes, sub_items for the respected tabs i.e. breaky, lunchy – Maddy Barakz Feb 27 '16 at 13:44

1 Answers1

0

If your problem is that you don't know the exact structure of the data in your json, you can use something like:

if([json isKindOfClass:[NSDictionnary class]) {
    // actions for a dictionnary
    id branch = json[@"your key"];
} else if([json isKindOfClass:[NSArray class]) {
    // actions for an array
    id branch = json[index];
}

Also the line if(jsonError == nil) make no sense, you sould do this after doing [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];