sahil dhiman
sahil dhiman

Reputation: 91

parsing of json url

I am doing the parsing of url and I followed the following processes:-

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In welcomemapViewController.h ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#import <UIKit/UIKit.h>

@interface welcomemapViewController : UIViewController
@property (strong, nonatomic) UITextField *txt;
@property (strong, nonatomic) NSString *typedtext;
@end

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

welcomemapViewController.m

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#import "welcomemapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#define kGOOGLE_API_KEY @"AIzaSyCGeIN7gCxU8baq3e5eL0DU3_JHeWyKzic"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kLatestSearchURL [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/textsearch/xml?query=chandigarh&sensor=true&key=kGOOGLE_API_KEY"]



@interface welcomemapViewController ()
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end
 @implementation NSDictionary(JSONCategories)

+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress
{
    NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
 }
-(NSData*)toJSON
 {
    NSError* error = nil;
    id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
    if (error != nil) return nil;
     return result;
}
@end

@implementation welcomemapViewController{
    GMSMapView *gmap;
}
@synthesize txt,typedtext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
 }

 - (void)viewDidLoad
 {
     [super viewDidLoad];

     GMSCameraPosition *cam = [GMSCameraPosition cameraWithLatitude:30.7343000 longitude:76.7933000 zoom:12];
     gmap = [GMSMapView mapWithFrame:CGRectMake(0, 60, 320, 480) camera:cam];
     [self.view addSubview:gmap];

     GMSMarker *marker = [[GMSMarker alloc] init];
     marker.position = CLLocationCoordinate2DMake(30.751288, 76.780899);
     marker.title = @"Sector -16";
     marker.snippet = @"Chandigarh";
     marker.map = gmap;

     UIButton *button    = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     button.frame        = CGRectMake(200, 65, 100, 40);
     [button setTitle:@"TITLE" forState:UIControlStateNormal];
     [button addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:button];

     CGRect frame2 = CGRectMake(10, 68, 200, 30);
     txt =[[UITextField alloc]initWithFrame:frame2];
     txt.placeholder = @"Search";
     txt.userInteractionEnabled = YES;
     txt.keyboardType = UIKeyboardTypeAlphabet;
     [txt setBorderStyle:UITextBorderStyleRoundedRect];

    [self.view addSubview:txt];

// Do any additional setup after loading the view from its nib.
}
-(IBAction)search:(id)sender
{
    dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL: kLatestSearchURL];
    NSString *data1 = [NSString stringWithUTF8String:[data bytes]];
    NSLog(@"Response data: %@", data1);
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data //1
                                                         options:kNilOptions
                                                           error:&error];
    NSLog(@"JSON Values: %@", json);

 });
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

My Array is returning me null value.

Can anybody tell me where am I wrong??

Upvotes: 0

Views: 105

Answers (1)

D.T
D.T

Reputation: 326

Seems like your response data might be returning null.

Can you try to print response data inside your fetchedData method to see what API is returning?

- (void)fetchedData:(NSData *)responseData {
    // Print response.
    NSString *data = [NSString stringWithUTF8String:[responseData bytes]];
    NSLog(@"Response data: %@", data);

    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSArray* latestLoans = [json objectForKey:@"loans"];

    NSLog(@"loans: %@", latestLoans);
}

EDIT - Your search method is trying to serialize Json object but response is returned in xml format. So, kLatestSearchURL should be updated to -

#define kLatestSearchURL [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=chandigarh&sensor=true&key=kGOOGLE_API_KEY"]

Upvotes: 1

Related Questions