【问题标题】:Facing difficulty in parsing google location response面临解析谷歌位置响应的困难
【发布时间】:2015-07-01 11:51:39
【问题描述】:

使用 iOS 8.0。

我正在使用Google API 获取位置。我想减少使用 NSPredicate 和 KeyPath 的编码。

我使用下面的链接来获取谷歌的回复, iPhone - Get City name from Latitude and Longtiude 使用用户@'Constantin Saulenco'的回答。

如果我是 Sql 开发人员,我会编写如下查询,

  1. (select address_components from MAIN_TABLE where types like 'postal_code') from this I will get NAME_TABLE rows.

2.select long_name from NAME_TABLE where types like 'locality' 

为此,我使用下面的代码,看了 @ztan 的回答后,我已经像这样更改了编码

编辑:回答

 NSArray *addressArray = json[@"results"];
NSPredicate *predicateForTypes=[NSPredicate predicateWithFormat:@"(types CONTAINS 'postal_code')"];
addressArray=([addressArray filteredArrayUsingPredicate:predicateForTypes][0])[@"address_components"];

NSPredicate *predicateForCity=[NSPredicate predicateWithFormat:@"(types CONTAINS 'locality')"];
NSString *cityName=([addressArray filteredArrayUsingPredicate:predicateForCity][0])[@"long_name"];

我有以下 JSON...

 {
        "address_components" =     (
                    {
                "long_name" = 221;
                "short_name" = 221;
                types =             (
                    "street_number"
                );
            },
                    {
                "long_name" = "Juniper Drive";
                "short_name" = "Juniper Dr";
                types =             (
                    route
                );
            },
                    {
                "long_name" = "North Kingstown";
                "short_name" = "North Kingstown";
                types =             (
                    locality,
                    political
                );
            },
                    {
                "long_name" = "Washington County";
                "short_name" = "Washington County";
                types =             (
                    "administrative_area_level_2",
                    political
                );
            },
                    {
                "long_name" = "Rhode Island";
                "short_name" = RI;
                types =             (
                    "administrative_area_level_1",
                    political
                );
            },
                    {
                "long_name" = "United States";
                "short_name" = US;
                types =             (
                    country,
                    political
                );
            },
                    {
                "long_name" = 02852;
                "short_name" = 02852;
                types =             (
                    "postal_code"
                );
            },
                    {
                "long_name" = 4308;
                "short_name" = 4308;
                types =             (
                    "postal_code_suffix"
                );
            }
        );
        "formatted_address" = "221 Juniper Drive, North Kingstown, RI 02852, USA";
        geometry =     {
            location =         {
                lat = "41.577761";
                lng = "-71.468383";
            };
            "location_type" = ROOFTOP;
            viewport =         {
                northeast =             {
                    lat = "41.5791099802915";
                    lng = "-71.46703401970849";
                };
                southwest =             {
                    lat = "41.5764120197085";
                    lng = "-71.4697319802915";
                };
            };
        };
        "place_id" = ChIJ563yzFex5YkRujoi28Fvzvo;
        types =     (
            "street_address"
        );
    },
    {
        "address_components" =     (
                    {
                "long_name" = "North Kingstown";
                "short_name" = "North Kingstown";
                types =             (
                    locality,
                    political
                );
            },
                    {
                "long_name" = "Washington County";
                "short_name" = "Washington County";
                types =             (
                    "administrative_area_level_2",
                    political
                );
            },
                    {
                "long_name" = "Rhode Island";
                "short_name" = RI;
                types =             (
                    "administrative_area_level_1",
                    political
                );
            },
                    {
                "long_name" = "United States";
                "short_name" = US;
                types =             (
                    country,
                    political
                );
            }
        );
        "formatted_address" = "North Kingstown, RI, USA";
        geometry =     {
            bounds =         {
                northeast =             {
                    lat = "41.6549521";
                    lng = "-71.4023083";
                };
                southwest =             {
                    lat = "41.497126";
                    lng = "-71.52244109999999";
                };
            };
            location =         {
                lat = "41.5568315";
                lng = "-71.4536835";
            };
            "location_type" = APPROXIMATE;
            viewport =         {
                northeast =             {
                    lat = "41.6549521";
                    lng = "-71.4023083";
                };
                southwest =             {
                    lat = "41.497126";
                    lng = "-71.52244109999999";
                };
            };
        };
        "place_id" = "ChIJgWUP-V-x5YkRO7Zie7NXWsI";
        types =     (
            locality,
            political
        );
    },2nd JSON Object, 3rd JSON Object ,......,....

我的问题是如何减少编码?使用 NSPRedicates 和 KeyPath ..

这就是我们如何解决问题......我在@ztan的回答中提到了这一点,请看一下,这将是有益的。

请看我的第二条评论......:)

【问题讨论】:

  • 我们可以使用 NSXMLParser 或任何其他解析器轻松解决这个复杂的 json。就像这个链接中的“用户@superGokuN的答案”中显示的一个很小的他stackoverflow.com/questions/10515639/…
  • 但坦率地说,解析google api,我们需要相应地分析和解析数据,我需要5-6个小时才能理解响应,我已经发布了,请任何人作为任何API,不要你认为我们应该从逻辑上解析谷歌响应?

标签: ios sql objective-c google-maps nspredicate


【解决方案1】:

您可以执行以下操作:

NSArray *addressArray = resultDict[@"results"];

NSPredicate *predicateFromTypes=[NSPredicate predicateWithFormat:@"(types CONTAINS 'postal_code') AND (types CONTAINS 'locality')"];

NSLog(@"\n%@", [addressArray[0][@"address_components"] filteredArrayUsingPredicate:predicateFromTypes][0][@"long_name"]);

【讨论】:

  • 感谢您的努力,但需要稍作修改,如果您有时间,请帮我修改,
  • 用postal_code过滤类型后,我会从Top 6数组对象中得到1个对象。
  • 得到这个对象后,我正在过滤这个对象。
  • 获取内容地址组件。
  • 这个对象是一个数组,我想要那个类型的对象有局部性。
【解决方案2】:

试试这个

NSArray *filtered = [YOURARRAY filteredArrayUsingPredicate:
    [NSPredicate predicateWithFormat:@"(types == %@)", @"locality "]];

【讨论】:

    猜你喜欢
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多