如何从此JSON响应中获取电话号码?

时间:2011-11-21 00:37:11

标签: iphone objective-c ios json nsdictionary

有点幽默:书呆子总是有问题获取电话号码:(。

我正试图从这个谷歌获取电话号码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"
}

目标IS:“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?

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

4 个答案:

答案 0 :(得分:2)

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

所以你可以看到“结果”会产生另一个字典。该词典的一个要素是“formatted_phone_number”。它包含一个字符串形式的电话号码。

剥去洋葱的层。

答案 1 :(得分:1)

尝试:

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

答案 2 :(得分:1)

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

试试这个,看看你做NSLog时是否出现了电话号码(@“phone:%@”,internationalPhone)

答案 3 :(得分: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