Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective c parse json from url request

I am trying to parse a json string requested from an api located at: http://www.physics.leidenuniv.nl/json/news.php

However, i am having trouble parsing this json. I get the following error: Unexpected end of file during string parse

I have looked for hours, but I can not find an answer to this problem.

My code snippet:

In my viewDidLoad:

NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"http://www.physics.leidenuniv.nl/json/news.php"]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

The delegate:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSMutableData *responseData = [[NSMutableData alloc] init];
[responseData appendData:data];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
}

Anybody know an answer to this problem so i can parse the json data?

like image 672
Wouter Willems Avatar asked Nov 19 '13 16:11

Wouter Willems


4 Answers

Do this way:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.

    [responseData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];

}
like image 85
Bhumeshwer katre Avatar answered Oct 17 '22 03:10

Bhumeshwer katre


Simple Way to store json-url data in dictionary.

 NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://query.yahooapis.com/v1/public/yql?q=select+%2A+from+weather.forecast+where+woeid%3D1100661&format=json"]];
    NSError *error=nil;
    id response=[NSJSONSerialization JSONObjectWithData:data options:
                 NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];

    if (error) {
        NSLog(@"%@",[error localizedDescription]);
    } else {
        _query = [response objectForKey:@"query"];
        NSLog(@"%@",_query); 

You can try this, so easy.

like image 39
Gangani Roshan Avatar answered Oct 17 '22 03:10

Gangani Roshan


I would recommend doing it this way:

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.physics.leidenuniv.nl/json/news.php"]];

__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                           json = [NSJSONSerialization JSONObjectWithData:data
                                                                  options:0
                                                                    error:nil];
                           NSLog(@"Async JSON: %@", json);
                       }];

Or if for whatever reason (not recommended) you want to run a synchronous request you could do:

NSData *theData = [NSURLConnection sendSynchronousRequest:request
                      returningResponse:nil
                                  error:nil];

NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:theData
                                                        options:0
                                                          error:nil];

NSLog(@"Sync JSON: %@", newJSON);
like image 38
Peter Foti Avatar answered Oct 17 '22 02:10

Peter Foti


//call this method
-(void)syncWebByGETMethod
{
      [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    NSString *urlString = [NSString stringWithFormat:@"http://www.yoursite.com"];
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
   [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]                          completionHandler:^(NSURLResponse * response, NSData * data, NSError * connectionError)
        {
         if (data)
         {
             id myJSON;
             @try {
                 myJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
             }
             @catch (NSException *exception) {
             }
             @finally {
             }
             jsonArray = (NSArray *)myJSON;

             NSLog(@"mmm %@",jsonArray);
         }
     }];
}
like image 43
Andal Priyadharshni V Avatar answered Oct 17 '22 04:10

Andal Priyadharshni V