0

I need to fetch JSON data from a URL in iOS6. How do I get these data into iOS? What if there is a URL to an image inside the data, how do I fetch these image too?

Michael
  • 32,527
  • 49
  • 210
  • 370
  • https://github.com/AFNetworking/AFNetworking – amar Jul 11 '13 at 08:20
  • Check out these link, [Json Parsing in iOS](http://stackoverflow.com/questions/5547311/how-do-i-parse-json-with-objective-c) and [Image download in iOS](http://stackoverflow.com/questions/6238139/ios-download-and-save-image-inside-app) – Amar Jul 11 '13 at 08:22
  • 1
    possible duplicate of [Getting Image from URL/server](http://stackoverflow.com/questions/1286096/getting-image-from-url-server) – Max MacLeod Jul 11 '13 at 08:30

2 Answers2

3

Parse the json and Put the url into an Array or NSString

 UIImageView *image1;
 NSString *responseString;//it will contain the url 
 image1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:responseString]]];
Vivek Sehrawat
  • 6,560
  • 2
  • 26
  • 39
0
/**
    HTTP CONSTANTS
 **/
#define TIMEOUT_INTERVAL 60
#define CONTENT_TYPE @"Content-Type"
#define URL_ENCODED @"application/x-www-form-urlencoded"
#define GET @"GET"
#define POST @"POST"

-(NSMutableURLRequest*)getNSMutableURLRequestUsingPOSTMethodWithUrl:(NSString *)url postData:(NSString*)_postData
{
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
    [req setHTTPMethod:POST];
    [req addValue:URL_ENCODED forHTTPHeaderField:CONTENT_TYPE];
    [req setHTTPBody: [_postData dataUsingEncoding:NSUTF8StringEncoding]];
    return req;
}


-(NSMutableURLRequest*)getNSMutableURLRequestUsingGetMethodWithUrl:(NSString*)url
{
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
    [req setHTTPMethod:GET];
    return req;
}


    NSString *_postData = {<Your postData>}
    NSMutableURLRequest *req = [self getNSMutableURLRequestUsingPOSTMethodWithUrl:LOGIN_URL postData:_postData];
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if (error)// it means server error
         {
             //error while connecting server
         }
         else
         {
             NSError *errorInJsonParsing;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorInJsonParsing];
             if(errorInJsonParsing) //error parsing in json
             {
                 //error in json parsing
             }
             else
             {
                 @try
                 {
                    NSLog(@"json==%@",json);
                    // json is basically in key value format which is NSDictionary
                    // if you get the url of image inside this json, then again hit the url to get the image ( u can get the image in NSData which is declared above )
                 }
                 @catch (NSException *exception)
                 {
                     //exception
                 }

             }
         }
     }];
Pradeep
  • 1,043
  • 7
  • 12