【问题标题】:Objective-C + JSON Parse objectForKey for Google Places API用于 Google Places API 的 Objective-C + JSON Parse objectForKey
【发布时间】:2013-04-12 08:54:18
【问题描述】:

我有这个:

    //request stuff
NSString *searchString = [NSString stringWithFormat:@"Sydney"];
NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=%@&sensor=true&key=mykeynotyours!",searchString];

NSURL *requestURL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];

//response
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

NSLog(@"%@",locationResults);

这会吐出一些结果,其中第一个是我想要的结果,但我无法像使用其他 API 那样获得 lat 和 lng:

NSString *stringLatitude = [locationResults objectForKey:@"lat"];

我怀疑它正在寻找的键是“结果”键的一个子集,我不知道如何告诉它:-(

结果:

    2013-04-12 18:47:49.784 Test[26984:c07] {
"html_attributions" =     (
);
results =     (
            {
        "formatted_address" = "Sydney NSW, Australia";
        geometry =             {
            location =                 {
                lat = "-33.8674869";
                lng = "151.2069902";
            };
            viewport =                 {
                northeast =                     {
                    lat = "-33.4245981";
                    lng = "151.3426361";
                };
                southwest =                     {
                    lat = "-34.1692489";
                    lng = "150.502229";
                };
            };
        };

感谢任何指导!

【问题讨论】:

    标签: ios objective-c xcode json parsing


    【解决方案1】:

    您可以使用以下代码访问几何 lat 和 lang

    NSString *stringLatitude = [[[[[locationResults objectForKey:@"results"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] valueForKey:@"lat"];
    NSString *stringLongitude = [[[[[locationResults objectForKey:@"results"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] valueForKey:@"lng"];
    

    【讨论】:

    • 它应该可以工作,不过让我再检查一下是否有错误..!
    • 在没有索引值的 NSDictionary 上抛出错误(locationResults 是一个 NSDictionary 对象)
    • 查看新答案..@NessyString
    【解决方案2】:

    使用这个

     NSString *stringLatitude = [[[[[locationResults objectForKey:@"results" ]objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] objectForKey:@"lat"];
     NSString *stringLongitude = [[[[[locationResults objectForKey:@"results" ]objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"] objectForKey:@"lng"];
    

    【讨论】:

      【解决方案3】:

      使用 iOS 7?将此放入您的项目中:https://github.com/gdyer/GDGooglePlaces

      那么……就用这个- (id)initWithPlaceCategory:(NSString *)_category urlStub:(NSString *)_stub lat:(float)\ _lat andLon:(float)_lon;

      映射、解析、查看、方向、分享等都为您处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-06
        • 1970-01-01
        • 2015-11-08
        • 2018-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多