FINAL EDIT WITH SOLUTION:
The code for resolve the problem:
-(void) atacarAbogados {
abogados = [[[NSArray alloc] init] autorelease]; NSLog(@"%@", userReceived); NSString *myRequestString = [NSString stringWithFormat:@"user=%@", userReceived]; NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL
URLWithString:@""]];
[request setHTTPMethod:@"POST"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [request setHTTPBody:myRequestData]; NSURLResponse *response; NSError *error; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response
error:&error];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]]; //NSLog(@"Contenido de content: %@", content); NSData *jsondata = [content dataUsingEncoding:NSUTF8StringEncoding]; NSError *jsonError = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:jsondata options:kNilOptions error:&jsonError]; if ([jsonObject isKindOfClass:[NSArray class]]) { NSLog(@"its an array!"); NSArray *jsonArray = (NSArray *)jsonObject; for (id item in jsonArray) { if ([item isKindOfClass:[NSDictionary class]]) { NSString *foundValue = [(NSDictionary*)item objectForKey:@"CodigoAbogado"]; if (foundValue) { NSLog(@"found 'CodigoAbogado' value: %@",foundValue); } } else { NSLog(@"item %@ is not a dictionary",item); } //do the same thing for another search key } } else { NSLog(@"its probably a dictionary"); NSDictionary *jsonDictionary = (NSDictionary *)jsonObject; NSString *foundValue = [jsonDictionary objectForKey:@"CodigoAbogado "]; NSLog(@"found 'CodigoAbogado' value %@", foundValue); //it return null if not found value for key 'COdigoabogado' } }
NEW EDIT: My new code, nad the app crashes, sorry...
NSLog(@"%@", userReceived); NSString *myRequestString = [NSString stringWithFormat:@"user=%@", userReceived]; NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL
URLWithString:@""]];
[request setHTTPMethod:@"POST"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [request setHTTPBody:myRequestData]; NSURLResponse *response; NSError *error; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response
error:&error];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]]; //NSLog(@"Contenido de content: %@", content); NSData *jsondata = [content dataUsingEncoding:NSUTF8StringEncoding]; NSError *e = nil; //NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsondata options: NSJSONReadingMutableContainers
error: &e];
NSError *jsonError = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:jsondata options:kNilOptions error:&jsonError]; if ([jsonObject isKindOfClass:[NSArray class]]) { NSLog(@"its an array!"); NSArray *jsonArray = (NSArray *)jsonObject; NSLog(@"jsonArray - %@",jsonArray); } else { NSLog(@"its probably a dictionary"); NSDictionary *jsonDictionary = (NSDictionary *)jsonObject; NSLog(@"jsonDictionary - %@",jsonDictionary); }}
The log:
2014-02-14 12:09:50.649 appAbogados[34863:f803] 1 2014-02-14 12:09:50.651 appAbogados[34863:f803] Administrador 2014-02-14 12:09:50.685 appAbogados[34863:f803] its an array! 2014-02-14 12:09:50.686 appAbogados[34863:f803] jsonArray - ( { 0 = 1; 1 = 24898; 10 = "Espa\U00f1a"; 11 = ""; 12 = Administrador; 13 = 1; 14 = 0; 15 = ""; 16 = 00; 17 = ""; 18 = ""; 19 = 0000000001; 2 = "JOAN LLUIS GONZALEZ FERRERI"; 20 = 00; 21 = 0; 22 = ""; 23 = ""; 24 = ""; 25 = " "; 26 = 0; 27 = 0; 28 = 0; 29 = 0; 3 = ""; 30 = 0; 31 = 0; 32 = 1; 33 = 0; 34 = 1; 35 = 3; 36 = 1; 37 = 000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000; 38 = "Nov 10 2011 11:32:54:000AM"; 39 = "\U00bd"; 4 = ""; 5 = ""; 6 = "juanluis@ferreriabogados.com"; 7 = ""; 8 = ""; 9 = ""; "Avis_Guardia" = 1; "Avis_Vista" = 0; CanalHabitualAbogado = 00; CertNombre = ""; ClienteSerieTurnoOficio = 00; ClienteTurnoOficio = 0000000001; CodigoAbogado = 1; CodigoColegioAbogado = 0; CodigoPostal = ""; Desc1 = 0; Desc2 = 0; Desc3 = 0; Desc4 = 0; Desc5 = 0; Desc6 = 0; Direccion = ""; ESPECIAL = "\U00bd"; EmpresaPredeterminadaEosCodigo = ""; EmpresaPredeterminadaEosNombre = ""; IPF = 0; IdOutLook = 000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000; ImportIdentificacion = ""; Mail = "juanluis@ferreriabogados.com"; MostrarInforme = 1; NIF = ""; Nombre = "JOAN LLUIS GONZALEZ FERRERI"; NombreColegioAbogado = ""; NombreComun = ""; Notas = ""; NumeroColegiado = 24898; Pais = "Espa\U00f1a"; Poblacion = ""; Provincia = ""; SerialCertificado = 1; Sexo = " "; "Tiempo_Avis" = 1; "Tiempo_Tipo" = 3; TipoVia = ""; UltimaModificacion = "Nov 10 2011 11:32:54:000AM"; Usuario = Administrador; } )
i have an script in php that response data in JSON.
When i receive the data, i have a string with the content, but i want to store these data in a dictionary because i want to access some values.
I have been trying to use NSJSONSerialization but my app crash everywhere. Im new and i have not idea please help.
My code is:
NSLog(@"%@", userReceived);
NSString *myRequestString = [NSString stringWithFormat:@"user=%@", userReceived];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@""]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:myRequestData];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"Contenido de content: %@", content);
The content of the variable "content" is:
2014-02-14 10:48:17.996 appAbogados[34082:f803] Contenido de content:
[{"0":"1","CodigoAbogado":"1","1":"24898","NumeroColegiado":"24898","2":"JOAN LLUIS GONZALEZ FERRERI","Nombre":"JOAN LLUIS GONZALEZ
FERRERI","3":"","NombreComun":"","4":"","NIF":"","5":"","Direccion":"","6":"juanluis@ferreriabogados.com","Mail":"juanluis@ferreriabogados.com","7":"","CodigoPostal":"","8":"","Poblacion":"","9":"","Provincia":"","10":"Espa\u00f1a","Pais":"Espa\u00f1a","11":"","Notas":"","12":"Administrador","Usuario":"Administrador","13":"1","SerialCertificado":"1","14":0,"CodigoColegioAbogado":0,"15":"","NombreColegioAbogado":"","16":"00","CanalHabitualAbogado":"00","17":"","EmpresaPredeterminadaEosCodigo":"","18":"","EmpresaPredeterminadaEosNombre":"","19":"0000000001","ClienteTurnoOficio":"0000000001","20":"00","ClienteSerieTurnoOficio":"00","21":0,"IPF":0,"22":"","TipoVia":"","23":"","ImportIdentificacion":"","24":"","CertNombre":"","25":"
","Sexo":"
","26":0,"Desc1":0,"27":0,"Desc2":0,"28":0,"Desc3":0,"29":0,"Desc4":0,"30":0,"Desc5":0,"31":0,"Desc6":0,"32":1,"Avis_Guardia":1,"33":0,"Avis_Vista":0,"34":1,"Tiempo_Avis":1,"35":3,"Tiempo_Tipo":3,"36":1,"MostrarInforme":1,"37":"000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000","IdOutLook":"000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000","38":"Nov
10 2011 11:32:54:000AM","UltimaModificacion":"Nov 10 2011
11:32:54:000AM","39":"\u00bd","ESPECIAL":"\u00bd"}]
The response of PHP script:
[{"0":"1","CodigoAbogado":"1","1":"24898","NumeroColegiado":"24898","2":"JOAN LLUIS GONZALEZ FERRERI","Nombre":"JOAN LLUIS GONZALEZ
FERRERI","3":"","NombreComun":"","4":"","NIF":"","5":"","Direccion":"","6":"juanluis@ferreriabogados.com","Mail":"juanluis@ferreriabogados.com","7":"","CodigoPostal":"","8":"","Poblacion":"","9":"","Provincia":"","10":"Espa\u00f1a","Pais":"Espa\u00f1a","11":"","Notas":"","12":"Administrador","Usuario":"Administrador","13":"1","SerialCertificado":"1","14":0,"CodigoColegioAbogado":0,"15":"","NombreColegioAbogado":"","16":"00","CanalHabitualAbogado":"00","17":"","EmpresaPredeterminadaEosCodigo":"","18":"","EmpresaPredeterminadaEosNombre":"","19":"0000000001","ClienteTurnoOficio":"0000000001","20":"00","ClienteSerieTurnoOficio":"00","21":0,"IPF":0,"22":"","TipoVia":"","23":"","ImportIdentificacion":"","24":"","CertNombre":"","25":"
","Sexo":"
","26":0,"Desc1":0,"27":0,"Desc2":0,"28":0,"Desc3":0,"29":0,"Desc4":0,"30":0,"Desc5":0,"31":0,"Desc6":0,"32":1,"Avis_Guardia":1,"33":0,"Avis_Vista":0,"34":1,"Tiempo_Avis":1,"35":3,"Tiempo_Tipo":3,"36":1,"MostrarInforme":1,"37":"000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000","IdOutLook":"000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000","38":"Nov
10 2011 11:32:54:000AM","UltimaModificacion":"Nov 10 2011
11:32:54:000AM","39":"\u00bd","ESPECIAL":"\u00bd"}]
Thanks very much.