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

发布于 2024-12-17 04:26:34 字数 2225 浏览 3 评论 0原文

一点幽默:书呆子在获取电话号码时总是遇到问题:(。

我正在尝试从 Google 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?

任何意见将不胜感激!

A little humour : Nerds always have problem getting phone number :(.

I am trying to get phone number from this Google places api response, Developing application on 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"
}

Target IS : "international_phone_number" : "+61 2 9374 4000"


My so far effort,

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

Any input will be appreciated! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撧情箌佬 2024-12-24 04:26:34

您已在上面的转储中列出了结构。最外层的结构是一个字典,有些字典条目包含数组,有些包含其他字典。 ({} 表示字典,[] 表示数组。)

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

剥去洋葱层。

You have the structure laid out in the above dump. The outermost structure is a dictionary, some of the dictionary entries contain arrays, some contain other dictionaries. ({} indicates a dictionary, [] indicates an array.)

So you can see that "result" yields another dictionary. One of the elements of that dictionary is "formatted_phone_number". It contains a phone number in the form of a string.

Peel the layers of the onion.

2024-12-24 04:26:34

尝试:

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

Try:

NSString *phoneNumber = [placeInfo objectForKey:@"formatted_phone_number"];
NSLog(@"Her Phone Number: %@", phoneNumber);
来日方长 2024-12-24 04:26:34
placeInfo = [results objectForKey:@"result"];
NSString *internationalPhone = [placeInfo objectForKey:@"international_phone_number"];

试试这个,看看当你执行 NSLog(@"phone: %@",internationalPhone) 时电话号码是否出现

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

Try this and see if the phone number appears when you do NSLog(@"phone: %@",internationalPhone)

半世晨晓 2024-12-24 04:26:34
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
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
衣神在巴黎 2024-12-24 04:26:34

您还可以通过使用来避免创建额外的变量

NSString* phoneNumber = [results objectForKeyPath@"result.international_phone_number"];

You could also avoid the creation of the extra variable by using

NSString* phoneNumber = [results objectForKeyPath@"result.international_phone_number"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文