【问题标题】:Google Geocoding API. I have a problem with parsing json response谷歌地理编码 API。我在解析 json 响应时遇到问题
【发布时间】:2011-06-28 14:12:54
【问题描述】:

我在解析 json 响应时遇到问题。我的错误是: [__NSArrayI objectForKey:]: unrecognized selector sent to instance。正如我所见,参数应该是 NSDictionary。为什么参数是 NSArray?

enter code hereNSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
NSString* requestStatus = [dictionary objectForKeyNotNull:@"status"];
if ([requestStatus isEqualToString:@"OK"])
{
    NSDictionary *params = [dictionary objectForKey:@"results"];
    NSString *addresses = [params objectForKey:@"formatted_address"];
}`enter code here`

我的 json 是:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]

}

【问题讨论】:

    标签: iphone json nsarray nsdictionary


    【解决方案1】:

    对于 @"results" 键,您有一个数组。

    所以你需要

    NSString *addresses = [[params objectAtIndex:0] objectForKey:@"formatted_address"];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-28
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多