NSDictionary 未获取第一项 JSON

NSDictionary Not Getting First Item JSON

也许有人可以提供帮助。我正在尝试在下面的 URL 中获取 google 地图位置和纬度的位置名称。正如您从 JSON 中看到的那样,它首先打开结果然后 address_components。我正在尝试获取 JSON 中的第一个 long_name 和 short_name 但我下面的代码只给我最后一个 long_name 和 short_name JSON。

也许有人有某种想法,我在这个阶段已经尝试了所有方法,但似乎没有任何方法可以解决问题。

谢谢,

柯蒂斯B

 -(void)getPlaceName
    {
        NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",53.348623,-6.243089];
        NSURL *url = [NSURL URLWithString:urlString];

        //Next we need a NSURLSession instance
        NSURLSession *session = [NSURLSession sharedSession];

        //All tasks (there are three, see documentation) are created from an NSURLSession instance
        //We want a DataTask
        NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            //This "block" (of code) will be executed when the call is complete

            //Serialize the JSON to Foundation objects
            NSDictionary *geocodeDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

            //Parse through the JSON to get to where we want, address_components array
            NSArray *resultsArray = geocodeDictionary[@"results"];
            NSDictionary *resultsDictionary = resultsArray[1];
            NSArray *addressComponents = resultsDictionary[@"address_components"];



            //Declare variables to hold desired results
            NSString *longName;
            NSString *shortName;

            //The address_components array contains many dictionaries,
            //we loop through each dictionary and check the types array
            for (NSDictionary *addressComponentDictionary in addressComponents) {

                    longName = addressComponentDictionary[@"long_name"];
                    shortName = addressComponentDictionary[@"short_name"];

            }

            //Test log to see we are correct
            NSLog(@"Long name for GetAddress is: %@", longName);
            NSLog(@"Short Name is: %@", shortName);
        }];

        [dataTask resume];
    }

JSON日志:

[8948:3198407] {
    results =     (
                {
            "address_components" =             (
                                {
                    "long_name" = "National College of Ireland";
                    "short_name" = "National College of Ireland";
                    types =                     (
                        premise
                    );
                },
                                {
                    "long_name" = "Mayor Street Lower";
                    "short_name" = "Mayor Street Lower";
                    types =                     (
                        route
                    );
                },
                                {
                    "long_name" = "International Financial Services Centre";
                    "short_name" = "International Financial Services Centre";
                    types =                     (
                        "sublocality_level_1",
                        sublocality,
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        locality,
                        political
                    );
                },
                                {
                    "long_name" = "Dublin 1";
                    "short_name" = "Dublin 1";
                    types =                     (
                        "postal_town"
                    );
                },
                                {
                    "long_name" = "Dublin City";
                    "short_name" = "Dublin City";
                    types =                     (
                        "administrative_area_level_2",
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Ireland;
                    "short_name" = IE;
                    types =                     (
                        country,
                        political
                    );
                }
            );
            "formatted_address" = "National College of Ireland, Mayor Street Lower, International Financial Services Centre, Dublin 1, Ireland";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "53.34914449999999";
                        lng = "-6.2420244";
                    };
                    southwest =                     {
                        lat = "53.3484171";
                        lng = "-6.2435218";
                    };
                };
                location =                 {
                    lat = "53.3487808";
                    lng = "-6.242773100000001";
                };
                "location_type" = ROOFTOP;
                viewport =                 {
                    northeast =                     {
                        lat = "53.3501297802915";
                        lng = "-6.241424119708499";
                    };
                    southwest =                     {
                        lat = "53.3474318197085";
                        lng = "-6.244122080291502";
                    };
                };
            };
            "place_id" = "ChIJ7fLaG40OZ0gRJsCRPZA1_iA";
            types =             (
                premise
            );
        },
                {
            "address_components" =             (
                                {
                    "long_name" = "Excise Walk";
                    "short_name" = "Excise Walk";
                    types =                     (
                        route
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        locality,
                        political
                    );
                },
                                {
                    "long_name" = "Dublin City";
                    "short_name" = "Dublin City";
                    types =                     (
                        "administrative_area_level_2",
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Ireland;
                    "short_name" = IE;
                    types =                     (
                        country,
                        political
                    );
                }
            );
            "formatted_address" = "Excise Walk, Dublin, Ireland";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "53.3492304";
                        lng = "-6.2434942";
                    };
                    southwest =                     {
                        lat = "53.3484317";
                        lng = "-6.243662899999999";
                    };
                };
                location =                 {
                    lat = "53.34883110000001";
                    lng = "-6.243578599999999";
                };
                "location_type" = "GEOMETRIC_CENTER";
                viewport =                 {
                    northeast =                     {
                        lat = "53.3501800302915";
                        lng = "-6.242229569708497";
                    };
                    southwest =                     {
                        lat = "53.3474820697085";
                        lng = "-6.244927530291502";
                    };
                };
            };
            "place_id" = "ChIJG_rIHI0OZ0gRHTz7pFf9r7o";
            types =             (
                route
            );
        },
                {
            "address_components" =             (
                                {
                    "long_name" = "North Dock";
                    "short_name" = "North Dock";
                    types =                     (
                        neighborhood,
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        locality,
                        political
                    );
                },
                                {
                    "long_name" = "Dublin City";
                    "short_name" = "Dublin City";
                    types =                     (
                        "administrative_area_level_2",
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Ireland;
                    "short_name" = IE;
                    types =                     (
                        country,
                        political
                    );
                }
            );
            "formatted_address" = "North Dock, Dublin, Ireland";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "53.3608191";
                        lng = "-6.1894282";
                    };
                    southwest =                     {
                        lat = "53.34522";
                        lng = "-6.2549399";
                    };
                };
                location =                 {
                    lat = "53.3497493";
                    lng = "-6.2306567";
                };
                "location_type" = APPROXIMATE;
                viewport =                 {
                    northeast =                     {
                        lat = "53.3608191";
                        lng = "-6.1894282";
                    };
                    southwest =                     {
                        lat = "53.34522";
                        lng = "-6.2549399";
                    };
                };
            };
            "place_id" = "ChIJr9h9M_oOZ0gRrycKzPZj46A";
            types =             (
                neighborhood,
                political
            );
        },
                {
            "address_components" =             (
                                {
                    "long_name" = "Dublin Northside";
                    "short_name" = "Dublin Northside";
                    types =                     (
                        neighborhood,
                        political
                    );
                },
                                {
                    "long_name" = Ashtown;
                    "short_name" = Ashtown;
                    types =                     (
                        "sublocality_level_1",
                        sublocality,
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        locality,
                        political
                    );
                },
                                {
                    "long_name" = "Dublin City";
                    "short_name" = "Dublin City";
                    types =                     (
                        "administrative_area_level_2",
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Ireland;
                    "short_name" = IE;
                    types =                     (
                        country,
                        political
                    );
                }
            );
            "formatted_address" = "Dublin Northside, Ashtown, Dublin, Ireland";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "53.4088667";
                        lng = "-6.0358715";
                    };
                    southwest =                     {
                        lat = "53.34558130000001";
                        lng = "-6.381769299999999";
                    };
                };
                location =                 {
                    lat = "53.3815507";
                    lng = "-6.1922052";
                };
                "location_type" = APPROXIMATE;
                viewport =                 {
                    northeast =                     {
                        lat = "53.4088667";
                        lng = "-6.0358715";
                    };
                    southwest =                     {
                        lat = "53.34558130000001";
                        lng = "-6.381769299999999";
                    };
                };
            };
            "place_id" = ChIJvUSkJbcPZ0gRyXXPH9MSQwk;
            types =             (
                neighborhood,
                political
            );
        },
                {
            "address_components" =             (
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        locality,
                        political
                    );
                },
                                {
                    "long_name" = "Dublin City";
                    "short_name" = "Dublin City";
                    types =                     (
                        "administrative_area_level_2",
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Ireland;
                    "short_name" = IE;
                    types =                     (
                        country,
                        political
                    );
                }
            );
            "formatted_address" = "Dublin, Ireland";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "53.42521010000001";
                        lng = "-6.0439235";
                    };
                    southwest =                     {
                        lat = "53.22343009999999";
                        lng = "-6.4474846";
                    };
                };
                location =                 {
                    lat = "53.3498053";
                    lng = "-6.2603097";
                };
                "location_type" = APPROXIMATE;
                viewport =                 {
                    northeast =                     {
                        lat = "53.42521010000001";
                        lng = "-6.0439235";
                    };
                    southwest =                     {
                        lat = "53.22343009999999";
                        lng = "-6.4474846";
                    };
                };
            };
            "place_id" = ChIJL6wn6oAOZ0gRoHExl6nHAAo;
            types =             (
                locality,
                political
            );
        },
                {
            "address_components" =             (
                                {
                    "long_name" = "Dublin City";
                    "short_name" = "Dublin City";
                    types =                     (
                        "administrative_area_level_2",
                        political
                    );
                },
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Ireland;
                    "short_name" = IE;
                    types =                     (
                        country,
                        political
                    );
                }
            );
            "formatted_address" = "Dublin City, Co. Dublin, Ireland";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "53.4111566";
                        lng = "-6.1131916";
                    };
                    southwest =                     {
                        lat = "53.2988569";
                        lng = "-6.3870807";
                    };
                };
                location =                 {
                    lat = "53.3603142";
                    lng = "-6.315054200000001";
                };
                "location_type" = APPROXIMATE;
                viewport =                 {
                    northeast =                     {
                        lat = "53.4111566";
                        lng = "-6.1131916";
                    };
                    southwest =                     {
                        lat = "53.2988569";
                        lng = "-6.3870807";
                    };
                };
            };
            "place_id" = ChIJv2RI7foRZ0gRwAKA8azHAAM;
            types =             (
                "administrative_area_level_2",
                political
            );
        },
                {
            "address_components" =             (
                                {
                    "long_name" = Dublin;
                    "short_name" = Dublin;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Ireland;
                    "short_name" = IE;
                    types =                     (
                        country,
                        political
                    );
                }
            );
            "formatted_address" = "Co. Dublin, Ireland";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "53.6347257";
                        lng = "-5.9962748";
                    };
                    southwest =                     {
                        lat = "53.1781971";
                        lng = "-6.5468798";
                    };
                };
                location =                 {
                    lat = "53.3824769";
                    lng = "-6.3133674";
                };
                "location_type" = APPROXIMATE;
                viewport =                 {
                    northeast =                     {
                        lat = "53.6347257";

                        lng = "-5.9962748";
                    };
                    southwest =                     {
                        lat = "53.1781971";
                        lng = "-6.5468798";
                    };
                };
            };
            "place_id" = ChIJv2RI7foRZ0gRwAKA8azHABg;
            types =             (
                "administrative_area_level_1",
                political
            );
        },
                {
            "address_components" =             (
                                {
                    "long_name" = Ireland;
                    "short_name" = IE;
                    types =                     (
                        country,
                        political
                    );
                }
            );
            "formatted_address" = Ireland;
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "55.4351345";
                        lng = "-5.994700099999999";
                    };
                    southwest =                     {
                        lat = "51.4199312";
                        lng = "-10.66958";
                    };
                };
                location =                 {
                    lat = "53.41291";
                    lng = "-8.24389";
                };
                "location_type" = APPROXIMATE;
                viewport =                 {
                    northeast =                     {
                        lat = "55.4351345";
                        lng = "-5.99471";
                    };
                    southwest =                     {
                        lat = "51.4199312";
                        lng = "-10.6694501";
                    };
                };
            };
            "place_id" = "ChIJ-ydAXOS6WUgRCPTbzjQSfM8";
            types =             (
                country,
                political
            );
        }
    );
    status = OK; }
//The address_components array contains many dictionaries,
        //we loop through each dictionary and check the types array
        for (NSDictionary *addressComponentDictionary in addressComponents) {

                longName = addressComponentDictionary[@"long_name"];
                shortName = addressComponentDictionary[@"short_name"];

        }

正如 Larme 所说,您的问题出在这里,而不是 longNameshortName。 创建 2 个数组:

NSMutableArray *longNameArray = [[NSMutableArray alloc] init]
NSMutableArray *shortNameArray = [[NSMutableArray alloc] init]
for (NSDictionary *addressComponentDictionary in addressComponents) {
    [longNameArray addObject:addressComponentDictionary[@"long_name"]];
    [shortNameArray addObject:addressComponentDictionary[@"short_name"]];
  }

这将为您提供在请求中找到的元素的完整列表。

更新 - 只获取第一个元素

如果只需要第一个元素,从数组中获取第一项

//This check will ensure that the array is not empty
if (addressComponents.count > 0) {
    NSDictionary *addressComponentDictionary = addressComponents[0];
    longName = addressComponentDictionary[@"long_name"];
    shortName = addressComponentDictionary[@"short_name"];
}

您的 for 循环遍历所有 address_components 并将它们的值分配给您的 longNameshortName 变量,因此当循环仅完成数组将被存储。如果你只想要第一组那么你可能想做这样的事情:

if ( addressComponents.count > 0 ) {
    NSDictionary *componentDict = addressComponents[0];
    longName = componentDict[@"long_name"];
    shortName = componentDict[@"short_name"];
}