我正在尝试使用AFHTTPRequestOperation建立一个儿子。 json是由坐标组成的,我想用它在我的视图中实现mapkit 这里是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);
}
这里是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;
}
);
}
这里我想插入一个“for”来获取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);
}
现在我的应用程序崩溃了。
设置这种情况的任何建议??
这是调试区域中的错误:
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)
答案 0 :(得分:0)
运行链接后,我发现了一个问题。你得到的JSON:
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;
}
);
}
所以,而不是
self.mapLocations = JSON;
你需要
self.mapLocations = [JSON objectForKey:@"json"];
或
self.mapLocations = JSON[@"json"];
然后,您将获得以下内容:
<__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;
}
)