简体   繁体   English

IOS 4.3如何从Web服务中正确解析Json响应并将其绑定到UITABLEVIEW

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

I'm new to iOS development. 我是iOS开发的新手。 What I'm trying to do is parse a JSON webservice (see sample output below). 我要做的是解析JSON Web服务(参见下面的示例输出)。 I would like to bind the said output to a UITableView. 我想将所述输出绑定到UITableView。 Could you show me the sample code on how to do this. 你能告诉我如何做到这一点的示例代码。 I'm using Xcode 3 with 4.3 sdk. 我正在使用带有4.3 sdk的Xcode 3。 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!). 好的,这里可能是初始代码,(关心Tim Stullich,谢谢男人!)。 I was able to pull da data from webservice. 我能够从webservice中提取da数据。 My next problem is how to bind it to a UITableView. 我的下一个问题是如何将它绑定到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. 使用IOS5,您可以使用NSJSONSerialization来解析JSON。

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

and you can do something like following to produce NSArray from Json Data. 并且您可以执行类似以下操作来从Json Data生成NSArray。

   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. 您可以使用诸如此类的json库(TouchJSON)将数据反序列化为可可对象。 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. 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. 您还可以看到: https//stackoverflow.com/questions/286087/best-json-library-to-use-when-developing-an-iphone-application它可以帮助您确定最适合您需求的内容。

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. 基本上你需要的是将你收到的数据解析成NSArray或NSMutableArray,然后实现UITableView使用的所需方法并从那里开始。 If you need more info let me know. 如果您需要更多信息,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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