0

嗨,我有以下格式的字符串

{
   results : [
      {
         address_components : [
            {
               long_name : 18,
               short_name : 18,
               types : [ street_number ]
            },
            {
               long_name : Great Russell Street,
               short_name : Great Russell St,
               types : [ route ]
            },
            {
               long_name : London,
               short_name : London,
               types : [ locality, political ]
            },
            {
               long_name : London Borough of Camden,
               short_name : London Borough of Camden,
               types : [ administrative_area_level_3, political ]
            },
            {
               long_name : Greater London,
               short_name : Gt Lon,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : England,
               short_name : England,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : United Kingdom,
               short_name : GB,
               types : [ country, political ]
            },
            {
               long_name : WC1B,
               short_name : WC1B,
               types : [ postal_code_prefix, postal_code ]
            },
            {
               long_name : London,
               short_name : London,
               types : [ postal_town ]
            }
         ],
         formatted_address : 18 Great Russell Street, London WC1B, UK,
         geometry : {
            location : {
               lat : 51.5183152,
               lng : -0.1262053
            },
            location_type : ROOFTOP,
            viewport : {
               northeast : {
                  lat : 51.51966418029149,
                  lng : -0.124856319708498
               },
               southwest : {
                  lat : 51.51696621970849,
                  lng : -0.127554280291502
               }
            }
         },
         types : [ street_address ]
      },
      {
         address_components : [
            {
               long_name : Suite 5,
               short_name : Suite 5,
               types : [ subpremise ]
            },
            {
               long_name : Russell House,
               short_name : Russell House,
               types : [ premise ]
            },
            {
               long_name : 37,
               short_name : 37,
               types : [ street_number ]
            },
            {
               long_name : Great Russell Street,
               short_name : Great Russell St,
               types : [ route ]
            },
            {
               long_name : Greater London,
               short_name : Gt Lon,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : United Kingdom,
               short_name : GB,
               types : [ country, political ]
            },
            {
               long_name : WC1B 3PP,
               short_name : WC1B 3PP,
               types : [ postal_code ]
            },
            {
               long_name : London,
               short_name : London,
               types : [ postal_town ]
            }
         ],
         formatted_address : Suite 5, Russell House, 37 Great Russell Street, London WC1B 3PP, UK,
         geometry : {
            location : {
               lat : 51.517836,
               lng : -0.127217
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 51.51918498029149,
                  lng : -0.125868019708498
               },
               southwest : {
                  lat : 51.51648701970849,
                  lng : -0.128565980291502
               }
            }
         },
         types : [ subpremise ]
      },
      {
         address_components : [
            {
               long_name : WC1B 3DE,
               short_name : WC1B 3DE,
               types : [ postal_code ]
            },
            {
               long_name : Greater London,
               short_name : Gt Lon,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : United Kingdom,
               short_name : GB,
               types : [ country, political ]
            },
            {
               long_name : London,
               short_name : London,
               types : [ postal_town ]
            }
         ],
         formatted_address : London WC1B 3DE, UK,
         geometry : {
            bounds : {
               northeast : {
                  lat : 51.52044859999999,
                  lng : -0.1257614
               },
               southwest : {
                  lat : 51.5184783,
                  lng : -0.1275254
               }
            },
            location : {
               lat : 51.5189698,
               lng : -0.1265003
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 51.5208124302915,
                  lng : -0.1252944197084979
               },
               southwest : {
                  lat : 51.5181144697085,
                  lng : -0.127992380291502
               }
            }
         },
         types : [ postal_code ]
      },
      {
         address_components : [
            {
               long_name : WC1B 3DG,
               short_name : WC1B 3DG,
               types : [ postal_code ]
            },
            {
               long_name : Greater London,
               short_name : Gt Lon,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : United Kingdom,
               short_name : GB,
               types : [ country, political ]
            },
            {
               long_name : London,
               short_name : London,
               types : [ postal_town ]
            }
         ],
         formatted_address : London WC1B 3DG, UK,
         geometry : {
            bounds : {
               northeast : {
                  lat : 51.52044859999999,
                  lng : -0.1257614
               },
               southwest : {
                  lat : 51.5184783,
                  lng : -0.1275254
               }
            },
            location : {
               lat : 51.5189698,
               lng : -0.1265003
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 51.5208124302915,
                  lng : -0.1252944197084979
               },
               southwest : {
                  lat : 51.5181144697085,
                  lng : -0.127992380291502
               }
            }
         },
         types : [ postal_code ]
      },
      {
         address_components : [
            {
               long_name : WC1B,
               short_name : WC1B,
               types : [ postal_code_prefix, postal_code ]
            },
            {
               long_name : Greater London,
               short_name : Gt Lon,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : United Kingdom,
               short_name : GB,
               types : [ country, political ]
            },
            {
               long_name : London,
               short_name : London,
               types : [ postal_town ]
            }
         ],

   status : OK
}

如何formatted_address从上面的字符串中检索值

我尝试了以下方式,但得到了 null 。

 NSData* data = [stringvalue dataUsingEncoding:NSUTF8StringEncoding];
            NSError* error;


            NSDictionary* json = [NSJSONSerialization  //getting NULL here
                                  JSONObjectWithData:data

                                  options:NSJSONReadingMutableContainers
                                  error:&error];

            //The results from Google will be an array obtained from the NSDictionary object with the key "results".
            NSArray* places = [json objectForKey:@"results"];
            for (int i=0; i<[places count]; i++)
            {
                NSDictionary* addr = [places objectAtIndex:i];
                 NSDictionary *loc = [addr objectForKey:@"formatted_address"];
                NSLog(@"loc=%@",loc);
            }
        }
4

1 回答 1

0
NSDictionary *loc = [addr objectForKey:@"formatted_address"];

我认为,“formatted_address”的值是NSStringobject not NSDictionary。所以请尝试这样:

NSString *loc = [addr objectForKey:@"formatted_address"];

另外,请注意键和值应包含双引号。

于 2013-11-08T13:37:38.897 回答