Friday, 24 January 2014

JSON Retrive from server

Format 1:

NSError *er;

NSDictionary  *Dic=[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"someURL"]]] options:NSJSONReadingMutableContainers error:&er];


Format 2:

NSString *urlString = [NSString stringWithFormat:@"%@loadPeople",ServiceUrl];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data1 =[NSData dataWithContentsOfURL:url];
    NSString *json_string = [[NSString allocinitWithData:data1 encoding:NSUTF8StringEncoding];
    
   // NSLog(@"json_string:%@",json_string);
    
       NSDictionary *dicc= (NSDictionary *) [parser objectWithString:json_string error:nil];

FORMAT3:


 NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    //NSString *myRequestString = [NSString stringWithFormat:@"countryId=1"];
    NSString *myRequestString = [NSString stringWithFormat:@"countryId=%@",countryId];
    //NSLog(@"test:%@",myRequestString);
    // [myRequestString stringByAppendingString:myParamString];
    NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8Stringlength:[myRequestString length]];
    [request setHTTPBody: requestData];
 NSHTTPURLResponse* response =[[NSHTTPURLResponse allocinit];
    NSError* error = [[NSError allocinit] ;
    
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseerror:&error];
    if (error)
    {
        
    }
    
    NSString *responseString = [[NSString allocinitWithBytes:[responseData bytes]
                                                        length:[responseData length]
                                                      encoding:NSUTF8StringEncoding];
    //NSLog(@"responseString:%@", responseString);
    

//    NSString *response1 = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
//    NSLog(@"response:%@",response1);
    
    SBJsonParser *parser = [[SBJsonParser alloc]init];
       
    NSMutableArray jason_array=[parser objectWithString:responseString];

No comments:

Post a Comment