简体   繁体   中英

IOS 4.3 How to properly Parse Json response from a Web-Service and bind it to UITABLEVIEW

I'm new to iOS development. What I'm trying to do is parse a JSON webservice (see sample output below). I would like to bind the said output to a UITableView. Could you show me the sample code on how to do this. I'm using Xcode 3 with 4.3 sdk. Thank you very much!

[{
    "event_id": "30",
    "bar_name": "Area 05 Superclub",
    "event_name": "test10",
    "date": "Dec 05, 2012 10:00 AM"
}, {
    "event_id": "27",
    "bar_name": "Area 05 Superclub",
    "event_name": "test7",
    "date": "Dec 02, 2012 10:00 AM"
}, {
    "event_id": "28",
    "bar_name": "Area 05 Superclub",
    "event_name": "test8",
    "date": "Dec 03, 2012 10:00 AM"
}, {
    "event_id": "29",
    "bar_name": "Area 05 Superclub",
    "event_name": "test9",
    "date": "Dec 04, 2012 10:00 AM"
}]

Ok guys, here's may intial code, (care of Tim Stullich, thanks man!). I was able to pull da data from webservice. My next problem is how to bind it to a UITableView. Hope you could help me again.

-(void)loadData{
    // Create new SBJSON parser object
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    // Prepare URL request to download statuses from Twitter
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http:xxx/gl_getEventsInformation.php"]];

    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    // parse the JSON response into an object
    // Here we're using NSArray since we're parsing an array of JSON status objects
    NSArray *statuses = [parser objectWithString:json_string error:nil];

    // Each element in statuses is a single status
    // represented as a NSDictionary
    for (NSDictionary *status in statuses)
    { NSLog(@"%@ - %@ - %@ - %@", [status objectForKey:@"event_id"],[status objectForKey:@"bar_name"],[status objectForKey:@"event_name"],[status objectForKey:@"date"]);}
}

With IOS5 you can use NSJSONSerialization for parsing the JSON.

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONWritingPrettyPrinted error:&error];

and you can do something like following to produce NSArray from Json Data.

   NSError *e = nil; NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];

    if (!jsonArray) 
       {   NSLog(@"Error parsing JSON: %@", e); } 
   else {    for(NSDictionary *item in jsonArray) 
         {
            NSLog(@"Item: %@", item);    
         }
       }

Hope this helps you

You can use a json library such as this (TouchJSON) in order to deserialize the data into cocoa objects. A simple workflow would be something like this:

#import "CJSONDeserializer.h"
...
NSString *jsonString = @"yourJSONHere";
NSData   *jsonData   = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError  *error      = nil;
NSArray  *jsonArray  = [[CJSONDeserializer deserializer] deserializeAsArray:jsonData error:&error];

PS. You can also see this: https://stackoverflow.com/questions/286087/best-json-library-to-use-when-developing-an-iphone-application It might help you decide on what is best for your needs.

What I would do is check this link out. Basically what you would need to is parse the data you received into preferably an NSArray or NSMutableArray and then implement the needed methods that are used by UITableView and go from there. If you need more info let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM