解析iphone应用程序中的json url时出错

时间:2012-06-28 09:52:26

标签: iphone

我是iPhone应用程序中的新bie。我想实现json解析。我已经尝试了net上提供的示例。但是在做json解析时无法找到如何获取url的索引值的确切方法有没有人早点做过。作为初学者,任何人都可以指导我。

等待积极回应

提前致谢 Iphone开发者

2 个答案:

答案 0 :(得分:0)

为您的视图执行

 NSURLRequest *request = [NSURLRequest requestWithURL:  
                             [NSURL URLWithString:Str_URL]];  
   // NSLog(@"\n\n\n request ===%@\n\n\n",request);
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if (theConnection) {

        // Create the NSMutableData to hold the received data.

        // receivedData is an instance variable declared elsewhere.

        responseData = [[NSMutableData data] init];

    } else {

        // Inform the user that the connection failed.

    }




#pragma mark NSURLConnection Delegate methods  
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    //NSLog(@"\n\n\n response ===%@\n\n\n",response);
    [responseData setLength:0];  
}  

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {  
    //NSLog(@"\n\n\n data ===%@\n\n\n",data);
    [responseData appendData:data];  
}  

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {  
    NSString *strerror = [NSString stringWithFormat:@"Connection failed: %@", [error description]];  
    NSLog(@"\n\n\n error ===%@\n\n\n",strerror);

}  

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {  

    //NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    if(responseData != nil)
    {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
       }

}  

答案 1 :(得分:0)

试试这个

-(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
{
    NSLog(@"ERROR with theConenction");
    [connection release];
    responseData = nil;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection release];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    responseData = nil;
    NSLog(@"response String= %@",responseString);

    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *data = (NSDictionary *) [parser objectWithString:responseString error:nil]; 

    NSDictionary *menu = (NSDictionary *) [data objectForKey:@"MainTag"];
    NSString *com=(NSString *) [menu objectForKey:@"yourSubTag"];
}