JSON 未被解析

发布于 2024-12-09 05:41:11 字数 3251 浏览 0 评论 0原文

我正在使用 GOogle Places API 开发一个程序,它有两个函数:searchPlaces 和 loadPlaceDetails。

函数 loadPlaceDetails 使用与 searchPlaces 函数相同的代码(经过修改),但它没有正确返回数据。

下面是 loadPlaceDetails 函数的代码:

- (NSString *)loadPlaceDetails{

    NSString *myJson = [[NSString alloc] initWithContentsOfURL:
                           [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/details/json?reference=CnRlAAAATXOCsnqUpz9GCU61PDw2GokDo2DTa_EWsUIDsfx5Fz5SF42iSarv-xE-smvnA6cY_kWbYIFte07Cu-_RsFvLewld_onmhaDvj_lsStNhoDzi_sTpOVhZywIH_8Y5YkrkudefaMF0J9vzUt_LfMzL2xIQXkCcDhJBwamOWFtvAXoAQRoUjwnYvrXeuYy6-ALt1enT1kRfDO4&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];

    if ([myJson length] == 0) {
        [myJson release];
        return @"Error";
    }

    // Create a dictionary from the JSON string
    NSDictionary *results = [myJson JSONValue];

    NSArray *place = [results objectForKey:@"results"];

    placeDetailName = [[NSMutableString alloc] init];
    placeDetailAddress = [[NSString alloc] init];
    placeDetailLat = [[NSString alloc] init];
    placeDetailLng = [[NSString alloc] init];
    placeDetailUrl = [[NSString alloc] init];
    placeDetailPhone = [[NSString alloc] init];

    for (NSDictionary *element in place)
    {
        NSString *name = [element objectForKey:@"name"];
        [placeDetailName stringByAppendingFormat:@"%@",name];
        NSLog(@"Nome do estabelecimento: %@",placeDetailName);

        NSString *address = [element objectForKey:@"formatted_address"];
        [placeDetailAddress stringByAppendingFormat:@"%@",address];
        NSLog(@"%@",address);

        NSString *phone = [element objectForKey:@"formatted_phone_number"];
        [placeDetailPhone stringByAppendingFormat:@"%@",phone];
        NSLog(@"%@",phone);

        NSString *url = [element objectForKey:@"url"];
        [placeDetailUrl stringByAppendingFormat:@"%@",url];
        NSLog(@"%@",url);
    }

    for (NSDictionary *result in [results objectForKey:@"results"])
    {
        NSDictionary *location = [[result objectForKey:@"geometry"] objectForKey:@"location"];
        NSString *latitude = [[location objectForKey:@"lat"] stringValue];
        NSString *longitude = [[location objectForKey:@"lng"] stringValue];
        [placeDetailLat stringByAppendingFormat:@"%@",latitude];
        [placeDetailLng stringByAppendingFormat:@"%@",longitude];
    }

    NSString *basicurl = @"http://www.(...)/(...)/directions.html";
    NSString *funcao = @"loaddetailplace";
    NSMutableString *placesURL = [NSMutableString string];

    [placesURL appendString:[NSString stringWithFormat:@"%@?function=",basicurl]];
    [placesURL appendString:[NSString stringWithFormat:@"%@&latorigem=",funcao]];
    [placesURL appendString:[NSString stringWithFormat:@"%@&lngorigem=",placeDetailLat]];
    [placesURL appendString:[NSString stringWithFormat:@"%@",placeDetailLng]];

    return placesURL;

}

当我在程序中的某个位置(同一类)调用它时,它返回 http://www.(...)/(...)/directions.html?function=loaddetailplace&latorigem=&lngorigem=

它不处理函数的其他部分。我不知道会发生什么,如果有人帮助我,我将不胜感激!

谢谢!

I'm developing a program using GOogle Places API and it has two functions: searchPlaces and loadPlaceDetails.

The function loadPlaceDetails is using the same code of the searchPlaces function (adapted) but it is not returning the data correctly.

Below, is the code of the loadPlaceDetails function:

- (NSString *)loadPlaceDetails{

    NSString *myJson = [[NSString alloc] initWithContentsOfURL:
                           [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/details/json?reference=CnRlAAAATXOCsnqUpz9GCU61PDw2GokDo2DTa_EWsUIDsfx5Fz5SF42iSarv-xE-smvnA6cY_kWbYIFte07Cu-_RsFvLewld_onmhaDvj_lsStNhoDzi_sTpOVhZywIH_8Y5YkrkudefaMF0J9vzUt_LfMzL2xIQXkCcDhJBwamOWFtvAXoAQRoUjwnYvrXeuYy6-ALt1enT1kRfDO4&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];

    if ([myJson length] == 0) {
        [myJson release];
        return @"Error";
    }

    // Create a dictionary from the JSON string
    NSDictionary *results = [myJson JSONValue];

    NSArray *place = [results objectForKey:@"results"];

    placeDetailName = [[NSMutableString alloc] init];
    placeDetailAddress = [[NSString alloc] init];
    placeDetailLat = [[NSString alloc] init];
    placeDetailLng = [[NSString alloc] init];
    placeDetailUrl = [[NSString alloc] init];
    placeDetailPhone = [[NSString alloc] init];

    for (NSDictionary *element in place)
    {
        NSString *name = [element objectForKey:@"name"];
        [placeDetailName stringByAppendingFormat:@"%@",name];
        NSLog(@"Nome do estabelecimento: %@",placeDetailName);

        NSString *address = [element objectForKey:@"formatted_address"];
        [placeDetailAddress stringByAppendingFormat:@"%@",address];
        NSLog(@"%@",address);

        NSString *phone = [element objectForKey:@"formatted_phone_number"];
        [placeDetailPhone stringByAppendingFormat:@"%@",phone];
        NSLog(@"%@",phone);

        NSString *url = [element objectForKey:@"url"];
        [placeDetailUrl stringByAppendingFormat:@"%@",url];
        NSLog(@"%@",url);
    }

    for (NSDictionary *result in [results objectForKey:@"results"])
    {
        NSDictionary *location = [[result objectForKey:@"geometry"] objectForKey:@"location"];
        NSString *latitude = [[location objectForKey:@"lat"] stringValue];
        NSString *longitude = [[location objectForKey:@"lng"] stringValue];
        [placeDetailLat stringByAppendingFormat:@"%@",latitude];
        [placeDetailLng stringByAppendingFormat:@"%@",longitude];
    }

    NSString *basicurl = @"http://www.(...)/(...)/directions.html";
    NSString *funcao = @"loaddetailplace";
    NSMutableString *placesURL = [NSMutableString string];

    [placesURL appendString:[NSString stringWithFormat:@"%@?function=",basicurl]];
    [placesURL appendString:[NSString stringWithFormat:@"%@&latorigem=",funcao]];
    [placesURL appendString:[NSString stringWithFormat:@"%@&lngorigem=",placeDetailLat]];
    [placesURL appendString:[NSString stringWithFormat:@"%@",placeDetailLng]];

    return placesURL;

}

When I call it somewhere in the program (same class), it returns http://www.(...)/(...)/directions.html?function=loaddetailplace&latorigem=&lngorigem=.

It is not processing the other parts of the function. I don't know what may be happening and would be grateful if someone helped me!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-12-16 05:41:11

该函数与返回的 JSON 不匹配。

例如,在您的函数中,您有:

NSArray *place = [results objectForKey:@"results"];

虽然返回数组的第一级包含类似内容:

"result": {
   "address_components": [],
   "formatted_address": "CRS 504 Bl B s/n lj 61 - Brasília - DF, 70331-525, Brasil",
   "formatted_phone_number": "(61) 3224-0625",
   "geometry": {},
   "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
   "id": "6c85c1faf37814d6d6ca8bea9576cbb49eece4f5",
   "international_phone_number": "+55 61 3224-0625",
   "name": "Banco Itaú S/A",
   "reference": "CnRlAAAA73YR15TKnyhpZQrt7Cgi6QfWkgtZD5jXemeoAIjeoS52LwfkxcjikEJZu0gkmg0519e9NWh5fuuGjzgN_B1qO1e7RuaI1ZgbpB5eXpflLWS1jJXUfjDfIflVyn017XXI56KQf0Qxd15WSiXFXDQu9xIQQ2M0WexDH9WwBgw0IvPCXxoUihDOTi3Qrx8RNRhmVJgivqnoxng",
   "types": [],
   "url": "http://maps.google.com/maps/place?cid=944480254308386018",
   "vicinity": "CRS 504 Bl B s/n lj 61 - Brasília"
 }

在第一级中,您有一个 NSDictionary (不是 NSArray)“结果”(不是结果)。我建议检查您正在使用的 URL 返回的 JSON 格式。

The function is not matching the returned JSON.

In your function, you have, for example:

NSArray *place = [results objectForKey:@"results"];

While the first level of the returned array contains something like:

"result": {
   "address_components": [],
   "formatted_address": "CRS 504 Bl B s/n lj 61 - Brasília - DF, 70331-525, Brasil",
   "formatted_phone_number": "(61) 3224-0625",
   "geometry": {},
   "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
   "id": "6c85c1faf37814d6d6ca8bea9576cbb49eece4f5",
   "international_phone_number": "+55 61 3224-0625",
   "name": "Banco Itaú S/A",
   "reference": "CnRlAAAA73YR15TKnyhpZQrt7Cgi6QfWkgtZD5jXemeoAIjeoS52LwfkxcjikEJZu0gkmg0519e9NWh5fuuGjzgN_B1qO1e7RuaI1ZgbpB5eXpflLWS1jJXUfjDfIflVyn017XXI56KQf0Qxd15WSiXFXDQu9xIQQ2M0WexDH9WwBgw0IvPCXxoUihDOTi3Qrx8RNRhmVJgivqnoxng",
   "types": [],
   "url": "http://maps.google.com/maps/place?cid=944480254308386018",
   "vicinity": "CRS 504 Bl B s/n lj 61 - Brasília"
 }

In the first level, you have a NSDictionary (not a NSArray) 'result' (not results). I would suggest to review the JSON format returned by the URL the you are using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文