Reputation: 49
In my app I need to parse JSON objects received from a remote server, not administered by me. What my app is getting from the server is a JSON string, that I convert to a NSDictionary. Then I am trying to retrieve the value of some of the JSON objects. At this point I am facing the following issue: I will take as example the value for the key "current_latitude: that I am retrieving.
If I make a NSLog with the retrieved string, I get this on my console:
VALUE=(
"-12.19061989"
)
Obviously, when I try to convert this string to a double, the app crashes.
This is the code so far:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
for (NSString* key in json) {
id value = [json objectForKey:key];
NSLog(@"VALUE=%@",[value valueForKey:@"current_latitude"]);
NSLog(@"VALUE=%@",[value valueForKey:@"current_longitude"]);
}
What should I do to get a normal string?
Thank you
EDITED
This is the complete piece of code to make the JSON request:
// request para descargar la posicion de los vehiculos disponibles
NSString *latitud = self.deviceLat;
NSString *longitud = self.deviceLon;
NSLog (@"latitud actual =%@",latitud);
NSLog (@"longitud actual =%@",longitud);
NSURL *apiURL = [NSURL URLWithString:
[NSString stringWithFormat:@"http://hidden here/?current_latitude=%@¤t_longitude=%@", latitud,longitud]];
NSURLRequest *request = [NSURLRequest requestWithURL:apiURL]; // this is using GET, for POST examples see the other answers here on this page
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if(data.length) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(responseString && responseString.length) {
NSLog(@"dATOS RECIBIDOS=%@", responseString);
NSError *jsonError;
NSData *objectData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
for (NSString* key in json) {
id value = [json objectForKey:key];
// do stuff
NSLog(@"VALUE=%@",[value valueForKey:@"current_latitude"]);
NSLog(@"VALUE=%@",[value valueForKey:@"current_longitude"]);
//ponemos en el punto deseado un marcador de tipo PinDisponible
NSString *latstring = [value valueForKey:@"current_latitude"];
NSString *lonstring = [value valueForKey:@"current_longitude"];
NSLog(@"LATITUD=%@", latstring);
NSLog(@"LONGITUD=%@", lonstring);
//double latdouble = [latstring doubleValue];
//double londouble = [lonstring doubleValue];
//NSLog(@"latdouble: %f", latdouble);
//NSLog(@"londouble: %f", londouble);
//CLLocationCoordinate2D vehiculo = [mapView centerCoordinate];
//vehiculo.latitude = latdouble;
//vehiculo.longitude = londouble;
//PinDisponible *vehiculoDisponible = [[PinDisponible alloc] initWithTitle:@"Vehiculo disponible" location:vehiculo];
// [self.mapView addAnnotation:vehiculoDisponible];
}
}
}
}];
}
Upvotes: 0
Views: 42
Reputation: 35616
Well, that's because it's not a string... it's an array with a string (the parenthesis you see at the logs are part of arrays desciption
), so I guess you could just grab
[[value valueForKey:@"current_latitude"] firstObject]
if you're positive that this is what you get from the backend. I hope that this makes sense
Upvotes: 1