我的问题是这个。当我解析我的JSON数据时,它将作为带有一个元素的NSSArray返回给我。在这一个元素中,它看起来像这样:
2015-07-14 20:45:38.467 ICBuses[51840:14872349] Bus Location: {
agencyname = Cambus;
agencytag = uiowa;
color = 00ff66;
directions = (
{
direction = Loop;
directiontag = loop;
stops = (
{
directiontitle = Loop;
stoplat = "41.659541";
stoplng = "-91.53775";
stopnumber = 1050;
stoptitle = "Main Library";
},
{
directiontitle = Loop;
stoplat = "41.6414999";
stoplng = "-91.55676";
stopnumber = 3200;
stoptitle = "Studio Arts Building";
},
{
directiontitle = Loop;
stoplat = "41.66504";
stoplng = "-91.54094";
stopnumber = 2100;
stoptitle = "Art Building";
},
{
directiontitle = Loop;
stoplat = "41.6662587";
stoplng = "-91.5405201";
stopnumber = 2106;
stoptitle = "Theatre Building";
},
{
directiontitle = Loop;
stoplat = "41.6679058";
stoplng = "-91.5401033";
stopnumber = 2120;
stoptitle = "Hancher/North Riverside";
},
{
directiontitle = Loop;
stoplat = "41.6660209";
stoplng = "-91.5406198";
stopnumber = 2105;
stoptitle = "Riverside & River St";
},
{
directiontitle = Loop;
stoplat = "41.66499";
stoplng = "-91.54106";
stopnumber = 2101;
stoptitle = "Art Building West";
},
{
directiontitle = Loop;
stoplat = "41.6612";
stoplng = "-91.5403422";
stopnumber = 145;
stoptitle = EPB;
}
);
}
);
我希望每个块都在那里元素...我该怎么做?下面是我的编码,它解析数据并将其存储到数组中。
将其存储到数组的代码:
self.routeInfo = [[API sharedAPI] routeInfoAgency:self.routeAgency
Route:self.route];
self.busLocation = [[API sharedAPI] busLocation:self.routeAgency
Route:self.route];
NSLog(@"Bus Location: %@", self.routeInfo);
下面是从服务器获取它并解析它的代码:
- (NSArray *) routeInfoAgency:(NSString *)agency Route:(NSString *)route {
NSError *error;
NSString *requestString = [NSString stringWithFormat:@"http://api.ebongo.org/route?format=json&agency=%@&route=%@&api_key=", agency, route];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",requestString, kApiKey]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if (!data) {
NSLog(@"Download Error: %@", error.localizedDescription);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Error %@", error.localizedDescription] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
return nil;
}
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSArray *JSONArray = [JSON valueForKeyPath:@"route.directions"];
return JSONArray;
}
我想在stops =之后是一个数组,其中每个元素是一个方向块标题,规定,停止,停止数字和停止标题。 谢谢
当我尝试使用floatValue时,我收到此错误: {
When I do that I'm getting this error {2015-07-15 10:31:05.821 ICBuses[56344:16107040] -[__NSArrayI floatValue]: unrecognized selector sent to instance 0x7f91fb5bdf90
2015-07-15 10:31:05.868 ICBuses[56344:16107040] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI floatValue]: unrecognized selector sent to instance 0x7f91fb5bdf90'
*** First throw call stack:
(
0 CoreFoundation 0x000000010bc53c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010b8ecbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010bc5b0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010bbb113c ___forwarding___ + 988
4 CoreFoundation 0x000000010bbb0cd8 _CF_forwarding_prep_0 + 120
5 ICBuses 0x000000010958f26e -[MapViewController viewDidLoad] + 3950
6 UIKit 0x000000010c17d1d0 -[UIViewController loadViewIfRequired] + 738
7 UIKit 0x000000010c17d3ce -[UIViewController view] + 27
8 UIKit 0x000000010c1a2257 -[UINavigationController _startCustomTransition:] + 633
9 UIKit 0x000000010c1ae37f -[UINavigationController _startDeferredTransitionIfNeeded:] + 386
10 UIKit 0x000000010c1aeece -[UINavigationController __viewWillLayoutSubviews] + 43
11 UIKit 0x000000010c2f96d5 -[UILayoutContainerView layoutSubviews] + 202
12 UIKit 0x000000010c0cc9eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
13 QuartzCore 0x000000010b18eed2 -[CALayer layoutSublayers] + 146
14 QuartzCore 0x000000010b1836e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
15 QuartzCore 0x000000010b183556 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16 QuartzCore 0x000000010b0ef86e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
17 QuartzCore 0x000000010b0f0a22 _ZN2CA11Transaction6commitEv + 462
18 QuartzCore 0x000000010b0f10d3 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
19 CoreFoundation 0x000000010bb86ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20 CoreFoundation 0x000000010bb86c00 __CFRunLoopDoObservers + 368
21 CoreFoundation 0x000000010bb7ca33 __CFRunLoopRun + 1123
22 CoreFoundation 0x000000010bb7c366 CFRunLoopRunSpecific + 470
23 GraphicsServices 0x000000010e727a3e GSEventRunModal + 161
24 UIKit 0x000000010c04c8c0 UIApplicationMain + 1282
25 ICBuses 0x000000010958e2cf main + 111
26 libdyld.dylib 0x000000010d481145 start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我认为这是因为for循环将其保存为具有一个元素的数组。 有什么建议吗?
答案 0 :(得分:0)
看起来它根本不是一个数组。看起来它实际上是一本字典。当您记录这样的集合时,您可以看到字典,因为它们标有{
,而数组标有(
。根据您的数据结构,这样的事情应该得到正确的成员:
NSDictionary *JSONDict = [JSON valueForKeyPath:@"route.directions"];
NSArray *directions = JSONDict[@"directions"];
NSDictionary *directionsDict = directions.firstObject; // seems to be an array with just 1 element.
NSArray *stops = directionsDict[@"stops"];
for(NSDictionary *stop in stops)
{
NSString *title = stop[@"directiontitle"];
NSString *stopLat = stop[@"stoplat"];
// etc
}
答案 1 :(得分:0)
在您的示例中,[JSON valueForKeyPath:@"route.directions"];
似乎返回NSSdictionary
,而不是NSArray
。
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
NSDictionary * directions = [json valueForKeyPath:@"route.directions"];
NSArray * stops = [directions valueForKeyPath:@"stops"];
然后,您可以遍历stops
数组。