如何使用ios中的json解析来获取数据

时间:2013-10-04 19:24:34

标签: iphone ios

我是iPhone开发人员的初学者。我想显示我在源代码下面使用的服务器的数据..

-(void)loadData:(id)sender
{
    self.requestdata=[NSMutableData data];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:kLatestKivaLoansURL]];
    [[NSURLConnection alloc]initWithRequest:request delegate:self];

    }

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

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

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

    [connection release];
    self.requestdata=nil;

}

#pragma mark-
#pragma process loan data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    [connection release];
    NSString *responsedata=[[NSString alloc]initWithData:requestdata encoding:NSUTF8StringEncoding];
    self.requestdata=nil;
    NSDictionary *respDict = [[responsedata JSONValue]objectForKey:@"nodes"];
    NSLog(@"Response Dict:%@",respDict);
    NSMutableArray *arraY = [[NSMutableArray alloc]init];
    arraY = [respDict mutableCopy];
    NSLog(@"My array:%@",arraY);
    NSString *mystr = [[[arraY objectAtIndex:0]valueForKey:@"node"]valueForKey:@"field_company_name_value"];
    NSLog(@"Mystrname:%@",mystr);
    NSArray *latestLoans=[(NSDictionary *)[responsedata JSONValue]objectForKey:@"responseData"];
    NSLog(@"latest_dictionary:%@",latestLoans);
    DisplayViewController *disp=[[DisplayViewController alloc]init];

    for (int i = 0; i< [[latestLoans valueForKey:@"entries"] count] ; i++) {

       // Search *aSearches = [[Search alloc] init];

        NSDictionary *tempDict = [[latestLoans valueForKey:@"entries"] objectAtIndex:i];

       // disp.link=[tempDict valueForKey:@"link"];

        /*link = [tempDict valueForKey:@"link"];
        aSearches.title = [tempDict valueForKey:@"title"];
        aSearches.description = [tempDict valueForKey:@"contentSnippet"];
        [appDelegate.search addObject:aSearches];*/

        [appDelegate.disparray addObject:tempDict];
    }

    NSLog(@"DisplayArray:%@",appDelegate.disparray);
    [self.view addSubview:disp.view];
   /* DisplayViewController *disp=[[DisplayViewController alloc]initWithNibName:@"DisplayViewController" bundle:nil];
    [self.navigationController pushViewController:disp animated:YES];*/



}

请提供适用于我的代码的任何建议或源代码

2 个答案:

答案 0 :(得分:0)

看看AFNetworking。它会为你完成这项工作。

有一种通过类AFJSONRequestOperation

处理JSON操作的特定方法

希望它有所帮助。

答案 1 :(得分:0)