1

我正在尝试获取此地址 =“8198 Snuffer School Rd, Gaithersburg, MD 20879, USA”

来自这个 json 请求,但无法成功。

{
  "name": "39.165110,-77.168550",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "8198 Snouffer School Rd, Gaithersburg, MD 20879, USA",
    "AddressDetails": {
   "Accuracy" : 8,
   "Country" : {
      "AdministrativeArea" : {
         "AdministrativeAreaName" : "MD",
         "Locality" : {
            "LocalityName" : "Gaithersburg",
            "PostalCode" : {
               "PostalCodeNumber" : "20879"
            },
            "Thoroughfare" : {
               "ThoroughfareName" : "Snouffer School Rd"
            }
         }
      },
      "CountryName" : "USA",
      "CountryNameCode" : "US"
   }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 39.1661825,
        "south": 39.1634846,
        "east": -77.1666884,
        "west": -77.1693864
      }
    },
    "Point": {
      "coordinates": [ -77.1685611, 39.1650977, 0 ]
    }
  } ]
}

我已经从状态中提取了代码,但不明白如何提取地址。

NSArray* Status = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Status"];

NSString *code = [Status objectForKey:@"code"];            
NSArray* Placemark  = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Placemark"];    
NSString *address = [Placemark objectForKey:@"address"]; // got error at this line.
4

3 回答 3

4

它是一个数组而不是 JSON 对象。尝试

NSString *address = [[Placemark objectAtIndex:0] objectForKey:@"address"]]; 
于 2012-04-27T13:17:51.397 回答
2

看起来地标条目返回一个数组,而不是字典(见方括号?)。

PS:请为您的变量使用小写字母 - 否则它们看起来像类:-)。

于 2012-04-27T13:18:58.900 回答
0

@Azhar :- 试试这个 -- id

IE

id 状态 = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Status"];

id 代码 = [状态 objectForKey:@"code"];
id Placemark = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Placemark"];
id address = [Placemark objectForKey:@"address"];

并找到所需的密钥。

于 2012-04-27T13:27:18.820 回答