来自URL连接的JSON对象显示在tableview中

时间:2012-05-18 04:53:17

标签: objective-c cocoa-touch uitableview ios4

#pragma mark -
#pragma mark Fetch loans from internet

-(void)loadData
{
    self.responseData = [NSMutableData data];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:
                                                          @"http://192.168.1.104:8080/Test/ItemGroup.jsp"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];
    self.responseData = nil;
}

#pragma mark -
#pragma mark Process loan data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    self.responseData = nil;


    array=[responseString JSONValue];
    NSMutableString *text=[NSMutableString stringWithString:@"Values:\n"];
    for (int i=0; i <[array count]; i++) {
        [text appendFormat:@"%@\n",[array objectAtIndex:i]];

        // NSLog(@"Values:""%@\n",array);

    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用RestKit(Cocoa RESTful Web服务框架)来获取数据,并使用object mapping功能将返回的数据映射到对象数组。

RESTKit对象映射wiki中给出的示例将JSON文档映射到文章实例数组中:https://github.com/RestKit/RestKit/wiki/Object-mapping