Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get info from NSData?

Tags:

ios

iphone

nsdata

I'm a new to iOS.

NSData:

{
"results" : [
  {
     "formatted_address" : "Proyezd Voskresenskiye Vorota, 3, Moscow, Russia, 109012",
     "geometry" : {
        "location" : {
           "lat" : 55.75622380,
           "lng" : 37.61855850
        } 

I need only "formatted_address", could you help me to make a NSString in which will be address? Sorry for stupid question.

like image 871
npmrtsv Avatar asked Feb 20 '23 15:02

npmrtsv


1 Answers

Your NSData is a JSON response, you need to create an NSDictionary to be able to access specific portions of the data. An NSDictionary just maps keys to values. To get the values you call – objectForKey:. In your case you have a dictionary as a value for the key "results". So in your case:

NSDictionary *results = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSDictionary *resultsDictionary = [[results objectForKey:@"results"] objectAtIndex:0];
NSString *formattedAddress = [resultsDictionary objectForKey:@"formatted_address"];
like image 151
Iñigo Beitia Avatar answered Mar 02 '23 17:03

Iñigo Beitia