English 中文(简体)
我如何从这名日本人的应对中获得电话号码?
原标题:How do I get phone number from this JSON response?

little: 内部电话总是存在问题:

我正试图从这一谷歌地方获得电话号码,以便作出一滴反应,开发Si-Si。

{
  "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”


我迄今为止的努力,

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

任何投入都将受到赞赏。 增 编

最佳回答

Try:

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

您的结构在上述垃圾堆放场上。 外部结构是字典,一些字典条目含有阵列,有些含有其他字典。 ({> 系指词典,[ ] 表示阵容。

因此,你可以看到,“结果”产生另一个词典。 该字典的一个要素是“照相——电话——号码”。 载有电话号码,以示形形式。

layer上层。

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

电话:%@”

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

你们也可以通过利用额外变量避免产生额外变量。

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




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签