-1

I am doing JSON parsing and I want to show my parsed data in a UITableView.

For that, I am trying to assign parsed data from NSMutableDictionary to NSArray to show in the table view but the array returns null.

Here my array returns null value;

NSMutableDictionary *tempDict1;
NSArray *arr = [[tempDict1 valueForKey:@"rates"]  componentsSeparatedByString:@";"];

code

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    [connection release];

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

    //    NSArray *latestrates = [[responseString JSONValue] objectForKey:@"rates"];
    [responseString release];

    values = [responseString JSONValue];
    array = [[NSMutableArray alloc] init];
    array = [values valueForKey:@"rates"];
    NSLog(@"array values:--> %@",array);


    tempDict1 = (NSMutableDictionary *)array;  
    arr = [[tempDict1 valueForKey:@"rates"]  componentsSeparatedByString:@";"];
    NSString *subStar = @"=";
    NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
    NSMutableArray *arrValues = [[NSMutableArray alloc] init];
    [arrTitle removeAllObjects];
    [arrValues removeAllObjects];
    for (int i=0; i<[arr count]-1; i++)
    {
        [arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]];
        [arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]];
        NSLog(@"arrTitle is:--> %@",arrTitle);
    }

    tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0];
    array = [values valueForKey:@"rates"];
    NSLog(@"tempDict--%@",tempDict1);

    [arr retain];
    [tbl_withData reloadData];

}
anilkumar07
  • 106
  • 1
  • 12

2 Answers2

0

Try editing fourth line in connectionDidFinishLoading to

values = [responseString JSONFragments];
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
  • thank's for quick reply i tryed that it's crashinh and showing'NSInvalidArgumentException', reason: '-[NSCFString JSONFragments]: – anilkumar07 Sep 14 '12 at 14:05
0
NSError *error = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Your data - %@",array);

Now you can get it according to data format.

EDIT

I think you also dont know how to get a webResponse.
So here is a way to get webResponse - First set XML delegate in your ViewController.h class and declare a NSMutableData globaly

@interface ViewController : UIViewController<NSXMLParserDelegate>
@property(nonatomic, retain)NSMutableData  *responseData;
@end

Now synthesized this responseData in your ViewController.m class

@synthesize responseData = _responseData;

Now you can send request on server in viewDidLoad: method its up to you in which method you want to send it.

-(void)viewDidLoad
{

    NSString *urlString = [NSString stringWithFormat:@"http://EnterYourURLHere"];

    NSURL *URL = [NSURL URLWithString:urlString];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]init];
    [urlRequest setURL:URL];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];

    NSURLConnection *urlConnection = [[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
    if(!urlConnection)
    {
        [[[UIAlertView alloc]initWithTitle:@"OOoopppssS !!" message:@"There is an error occured. Please check your internet connection or try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
    }

}

#pragma mark - Parsing delegate methods

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.responseData = [[NSMutableData alloc]init];
    [self.responseData setLength:0];
}

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

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Now parse your data here -
    NSError *error = nil;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"Your data - %@",array);
}
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • When you have your `responseData` you can use it. Use this in `- (void)connectionDidFinishLoading:(NSURLConnection *)connection` method after getting your `responseData`. – TheTiger Sep 15 '12 at 05:33
  • am geting error at NSJSONSerialization (uncatch exception NSJSONSerialization) plz give me url where can i get NSJSONSerialization class files – anilkumar07 Sep 15 '12 at 09:45
  • i cant access NSJSONSerialization it's show error i think i need to add NSJSONSerialization class. – anilkumar07 Sep 15 '12 at 10:05
  • Nope, you dont need to add anything for this. What is error and what is your URL ? – TheTiger Sep 15 '12 at 10:49
  • error is use of undeclared identifier NSJSONSerialization,this is my url http://openexchangerates.org/latest.json – anilkumar07 Sep 15 '12 at 12:31