Ios 将JSON解析为单个NSArray元素

Ios 将JSON解析为单个NSArray元素,ios,json,nsarray,nsdictionary,Ios,Json,Nsarray,Nsdictionary,我的问题是这个。当我解析我的JSON数据时,它会作为一个包含一个元素的NSSArray返回给我。在这一元素中,它看起来如下所示: 2015-07-14 20:45:38.467 ICBuses[51840:14872349] Bus Location: { agencyname = Cambus; agencytag = uiowa; color = 00ff66; directions = ( {

我的问题是这个。当我解析我的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=之后的内容位于一个数组中,其中每个元素都是方向块title、regulate、stoping、stop number和stoptitle。 多谢各位

当我尝试使用floatValue时,出现以下错误: {

我认为这是因为for循环将其保存为一个包含一个元素的数组。
有什么建议吗?

看起来它根本不是一个数组。它看起来实际上是一个字典。当您记录这样的集合时,您可以看到字典,因为它们用{标记,数组用标记。根据您的数据结构,类似这样的内容应该可以获得正确的成员:

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
}

在您的示例中,[JSON valueForKeyPath:@route.directions];似乎返回NSSdictionary,而不是NSArray


然后,您可以遍历stops数组。

它可能无法回答问题,但是,为什么不使用它将使调用web服务和解析的过程更加容易。这是我的第一个应用程序,我想了解这个过程。如果@Dima answer对您有效,您应该接受它。如果我想返回stop[@stoplat],该怎么办;作为浮点?使用NSString转换方法。只需执行stopLat.doubleValue或stopLat.floatValue即可。
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
}
    NSDictionary *json = [NSJSONSerialization
                  JSONObjectWithData:data
                  options:NSJSONReadingMutableContainers
                  error:&error];
    NSDictionary * directions = [json valueForKeyPath:@"route.directions"];
    NSArray * stops = [directions valueForKeyPath:@"stops"];