Search code examples
iosjsonurlarraysafhttprequestoperation

How do I use data from a json array?


i’m trying to set up a son using AFHTTPRequestOperation. that json is compose by coordinates and I want use that to implement mapkit inside my view here the cose.

NSURL *URL = [NSURL URLWithString:@"http://www.json-generator.com/api/json/get/cvBRYullDS?indent=2"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id JSON) {
        self.mapLocations = JSON;
    NSLog(@"%@",self.mapLocations);
}

here the NSLog:

json =     (
                {
            lat = "45.1041";
            lng = "10.8362";
            name = Swifty;
        },
                {
            lat = "45.1008";
            lng = "10.8643";
            name = Gianluca;
        },
                {
            lat = "45.1046";
            lng = "10.8681";
            name = Carpi;
        }
    );
}

here I want insert a “for” to get pins inside a mapkit:

NSURL *URL = [NSURL URLWithString:@"http://www.json-generator.com/api/json/get/cvBRYullDS?indent=2"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id JSON) {
        self.mapLocations = JSON;
        MKPointAnnotation *annotation;

        for(NSDictionary *location in self.mapLocations) {
            CLLocationCoordinate2D annotationCoordinate =
            CLLocationCoordinate2DMake([location[@"lat"] doubleValue],
                                       [location[@"lng"] doubleValue]);
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
            annotation.coordinate = annotationCoordinate;
            annotation.title = location[@"name"];
            annotation.subtitle = nil;
            [self.mapView addAnnotation:annotation];
        }

        NSLog(@"aaaaaaaaaaaaaaaaa%@",self.mapLocations);
    }

Now my app crashed.

Any advices to set that situation ??

This is the error in the debug area:

015-01-09 13:48:46.941 pettoiletmaps[6393:539721] <+44.90000000,+10.80000000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 1/9/15, 1:48:44 PM Central European Standard Time
2015-01-09 13:48:47.211 pettoiletmaps[6393:539721] -[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7ffd8ae416d0
2015-01-09 13:48:47.213 pettoiletmaps[6393:539721] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7ffd8ae416d0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010af2df35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010abc6bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010af3504d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010ae8d27c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010ae8ce18 _CF_forwarding_prep_0 + 120
    5   pettoiletmaps                       0x000000010898b67e __29-[ViewController viewDidLoad]_block_invoke + 526
    6   pettoiletmaps                       0x000000010899df18 __64-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke46 + 40
    7   libdispatch.dylib                   0x000000010c56bba6 _dispatch_call_block_and_release + 12
    8   libdispatch.dylib                   0x000000010c5897f4 _dispatch_client_callout + 8
    9   libdispatch.dylib                   0x000000010c5728fb _dispatch_main_queue_callback_4CF + 949
    10  CoreFoundation                      0x000000010ae95fe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    11  CoreFoundation                      0x000000010ae58eeb __CFRunLoopRun + 2043
    12  CoreFoundation                      0x000000010ae58486 CFRunLoopRunSpecific + 470
    13  GraphicsServices                    0x000000010d4fd9f0 GSEventRunModal + 161
    14  UIKit                               0x0000000108e71420 UIApplicationMain + 1282
    15  pettoiletmaps                       0x000000010899f603 main + 115
    16  libdyld.dylib                       0x000000010c5be145 start + 1
    17  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Solution

  • After running your link I have found an issue. The JSON you got :

    JSON: {
        json =     (
                    {
                lat = "45.1041";
                lng = "10.8362";
                name = cilly;
            },
                    {
                lat = "45.1008";
                lng = "10.8643";
                name = Gian;
            },
                    {
                lat = "45.1046";
                lng = "10.8681";
                name = Fabio;
            }
        );
    }
    

    So, instead of self.mapLocations = JSON;

    You need self.mapLocations = [JSON objectForKey:@"json"];

    or self.mapLocations = JSON[@"json"];

    Then, you will get the following:

    <__NSCFArray 0x7ff752c33da0>(
    {
        lat = "45.1041";
        lng = "10.8362";
        name = cilly;
    },
    {
        lat = "45.1008";
        lng = "10.8643";
        name = Gian;
    },
    {
        lat = "45.1046";
        lng = "10.8681";
        name = Fabio;
    }
    )