2011-11-21 32 views
1

一个小幽默:。书呆子总是有问题让电话号码:(如何从此JSON响应中获取电话号码?

我试图得到电话号码从这个谷歌Places API的响应,在iOS上开发应用

{ 
    "html_attributions" : [], 
    "result" : { 
    "address_components" : [ 
     { 
     "long_name" : "48", 
     "short_name" : "48", 
     "types" : [ "street_number" ] 
     }, 
     { 
     "long_name" : "Pirrama Road", 
     "short_name" : "Pirrama Road", 
     "types" : [ "route" ] 
     }, 
     { 
     "long_name" : "Pyrmont", 
     "short_name" : "Pyrmont", 
     "types" : [ "locality", "political" ] 
     }, 
     { 
     "long_name" : "NSW", 
     "short_name" : "NSW", 
     "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
     "long_name" : "AU", 
     "short_name" : "AU", 
     "types" : [ "country", "political" ] 
     }, 
     { 
     "long_name" : "2009", 
     "short_name" : "2009", 
     "types" : [ "postal_code" ] 
     } 
    ], 
    "formatted_address" : "5/48 Pirrama Road, Pyrmont NSW, Australia", 
    "formatted_phone_number" : "(02) 9374 4000", 
    "geometry" : { 
     "location" : { 
     "lat" : -33.8669710, 
     "lng" : 151.1958750 
     } 
    }, 
    "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", 
    "id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7", 
    "international_phone_number" : "+61 2 9374 4000", 
    "name" : "Google Sydney", 
    "rating" : 4.60, 
    "reference" : "CnRlAAAAAfV6JIqSzL8Cf4VnXn0EaI1d5k3IPhdkEonq0MxiUbQFFSVuptVbXbNH4mrevb0bc7G8yWqTUv76i4KTuO_Wf3OrRHjCJJwzQ0mNLjbYGSVqy2eqyrgOUkl6S_sJfTbHzWZYrfPy7KZaet0mM5S6thIQJYuy5v_JD--ZxXEJLWTQRRoU5UaciXBBo89K-bce18Ii9RsEIws", 
    "types" : [ "store", "establishment" ], 
    "url" : "http://maps.google.com/maps/place?cid=10281119596374313554", 
    "vicinity" : "5/48 Pirrama Road, Pyrmont", 
    "website" : "http://www.google.com.au/" 
    }, 
    "status" : "OK" 
} 

目标: “international_phone_number”: “+61 2 9374 4000”


我迄今为止的努力,

NSDictionary *results = [responseString JSONValue]; 
    placeInfo = [results objectForKey:@"result"]; 
//Getting result details but than I am stuck how to get to that international phone number? 

任何输入将不胜感激!谢谢

回答

1

尝试:

NSString *phoneNumber = [placeInfo objectForKey:@"formatted_phone_number"]; 
NSLog(@"Her Phone Number: %@", phoneNumber); 
+0

您可以al所以请避免使用NSString * phoneNumber = [results objectForKeyPath @“result.formatted_phone_number”]创建额外变量; –

2

你有上述转储布局的结构。最外层的结构是一个字典,一些字典条目包含数组,一些包含其他字典。 ({}表示字典,[]表示数组)。

因此,您可以看到“结果”会生成另一个字典。该字典的一个元素是“formatted_phone_number”。它包含一个字符串形式的电话号码。

剥离洋葱的层。

1
placeInfo = [results objectForKey:@"result"]; 
NSString *internationalPhone = [placeInfo objectForKey:@"international_phone_number"]; 

试试这个,看看,当你这样做的NSLog出现的电话号码(@ “电话:%@”,internationalPhone)

0
NSError *parseError = nil; 

// Your test json as NSString 
NSString * json = @"{ \"html_attributions\" : [], \"result\" : { \"address_components\" : [ { \"long_name\" : \"48\", \"short_name\" : \"48\", \"types\" : [ \"street_number\" ] }, { \"long_name\" : \"Pirrama Road\", \"short_name\" : \"Pirrama Road\", \"types\" : [ \"route\" ] }, { \"long_name\" : \"Pyrmont\", \"short_name\" : \"Pyrmont\", \"types\" : [ \"locality\", \"political\" ] }, { \"long_name\" : \"NSW\", \"short_name\" : \"NSW\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"AU\", \"short_name\" : \"AU\", \"types\" : [ \"country\", \"political\" ] }, { \"long_name\" : \"2009\", \"short_name\" : \"2009\", \"types\" : [ \"postal_code\" ] } ], \"formatted_address\" : \"5/48 Pirrama Road, Pyrmont NSW, Australia\", \"formatted_phone_number\" : \"(02) 9374 4000\", \"geometry\" : { \"location\" : { \"lat\" : -33.8669710, \"lng\" : 151.1958750 } }, \"icon\" : \"http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png\", \"id\" : \"4f89212bf76dde31f092cfc14d7506555d85b5c7\", \"international_phone_number\" : \"+61 2 9374 4000\", \"name\" : \"Google Sydney\", \"rating\" : 4.60, \"reference\" : \"CnRlAAAAAfV6JIqSzL8Cf4VnXn0EaI1d5k3IPhdkEonq0MxiUbQFFSVuptVbXbNH4mrevb0bc7G8yWqTUv76i4KTuO_Wf3OrRHjCJJwzQ0mNLjbYGSVqy2eqyrgOUkl6S_sJfTbHzWZYrfPy7KZaet0mM5S6thIQJYuy5v_JD--ZxXEJLWTQRRoU5UaciXBBo89K-bce18Ii9RsEIws\", \"types\" : [ \"store\", \"establishment\" ], \"url\" : \"http://maps.google.com/maps/place?cid=10281119596374313554\", \"vicinity\" : \"5/48 Pirrama Road, Pyrmont\", \"website\" : \"http://www.google.com.au/\" }, \"status\" : \"OK\" }"; 

// Converting to data and stripping last 0x0 byte 
NSData* aweData = [json dataUsingEncoding:NSUTF8StringEncoding]; 
[aweData subdataWithRange:NSMakeRange(0, [aweData length] - 1)]; 

id jsonObject = [NSJSONSerialization JSONObjectWithData:aweData options:NSJSONReadingAllowFragments error:&parseError]; 

NSString* result = [[jsonObject objectForKey:@"result"] objectForKey:@"international_phone_number"]; // +61 2 9374 4000 
0

你也可以避免通过使用创建额外变量

NSString* phoneNumber = [results [email protected]"result.international_phone_number"];