David David - 6 months ago 11
Objective-C Question

Incorrect conversion from object to JSON?

I need to send to a web service this JSON by POST:

{
"plantilla" : "IL3\/il3_prof",
"contacto" : {
"email" : "vgonzalez@ub.edu",
"nombre" : "Vanesa",
"movilPersonal" : "934037680",
"apellidos" : "Gonzalez Jimenez."
"direccionTrabajoDTO" : {
"direccion" : "Feixa Llarga, S\/N 08970 Hospitalet De Llobregat (Barcelona), España"
},
},
"valoresPlantilla" : [
{
"key" : "nombre",
"value" : "Vanesa Gonzalez Jimenez."
},
{
"key" : "curso",
"value" : "Curs de test per Tecnologia"
},
{
"key" : "linked",
"value" : "es.linkedin.com\/in\/"
},
{
"key" : "direccion",
"value" : "Feixa Llarga, S\/N 08970 Hospitalet De Llobregat (Barcelona), España"
},
{
"key" : "email",
"value" : "vgonzalez@ub.edu"
},
{
"key" : "telefono",
"value" : "934037680"
}
]
}


I created the object Tarjeta with values:

Tarjeta *tarjeta = [[Tarjeta alloc] init];
tarjeta.plantilla = @"IL3/il3_prof";

Contacto *contacto = [[Contacto alloc] init];
contacto.nombre = @"Vanesa";
contacto.apellidos = @"Gonzalez Jimenez.";
contacto.email = @"vgonzalez@ub.edu";
contacto.movilPersonal = @"934037680";

DireccionTrabajo *direccionTrabajo = [[DireccionTrabajo alloc] init];
direccionTrabajo.direccion = @"Feixa Llarga, S/N 08970 Hospitalet De Llobregat (Barcelona), España";
contacto.direccionTrabajo = direccionTrabajo;

tarjeta.contacto = contacto;

ValorPlantilla *nombre = [[ValorPlantilla alloc] init];
nombre.key = @"nombre";
nombre.value = @"Vanesa Gonzalez Jimenez.";
ValorPlantilla *curso = [[ValorPlantilla alloc] init];
curso.key = @"curso";
curso.value = @"Curs de test per Tecnologia";
ValorPlantilla *linked = [[ValorPlantilla alloc] init];
linked.key = @"linked";
linked.value = @"es.linkedin.com/in/";
ValorPlantilla *direccion = [[ValorPlantilla alloc] init];
direccion.key = @"direccion";
direccion.value = @"Feixa Llarga, S/N 08970 Hospitalet De Llobregat (Barcelona), España";
ValorPlantilla *email = [[ValorPlantilla alloc] init];
email.key = @"email";
email.value = @"vgonzalez@ub.edu";
ValorPlantilla *telefono = [[ValorPlantilla alloc] init];
telefono.key = @"telefono";
telefono.value = @"934037680";

tarjeta.valoresPlantilla = [NSArray arrayWithObjects:nombre, curso, linked, direccion, email, telefono, nil];

return tarjeta;


After this, I do a conversion from object to JSON with this method:

NSMutableDictionary *tarjetaDict = [[NSMutableDictionary alloc] init];
[tarjetaDict setValue:tarjeta.plantilla forKey:@"plantilla"];

NSMutableDictionary *contactoDict = [[NSMutableDictionary alloc] init];
[contactoDict setValue:tarjeta.contacto.nombre forKey:@"nombre"];
[contactoDict setValue:tarjeta.contacto.apellidos forKey:@"apellidos"];
[contactoDict setValue:tarjeta.contacto.email forKey:@"email"];

[contactoDict setValue:tarjeta.contacto.movilPersonal forKey:@"movilPersonal"];

NSMutableDictionary *direccionDict = [[NSMutableDictionary alloc] init];
[direccionDict setValue:tarjeta.contacto.direccionTrabajo.direccion forKey:@"direccion"];

[contactoDict setValue:direccionDict forKey:@"direccionTrabajoDTO"];

[tarjetaDict setValue:contactoDict forKey:@"contacto"];

[tarjetaDict setValue:tarjeta.valoresPlantilla forKey:@"valoresPlantilla"];

return tarjetaDict;


When I finish this process, I send the NSMutableDictionary like JSON to the web service, but it says that's incorrect and this is the error.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (ValorPlantilla)'


I'm doing anything wrong?

Answer

Add this method to your ValorPlantilla class

-(NSMutableDictionary *)getobject
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

    [dict setObject:self.key forKey:@“key"];
    [dict setObject:self.value forKey:@“value"];

    return dict;
}

And Change this line

tarjeta.valoresPlantilla = [NSArray arrayWithObjects:[nombre getobject], [curso, linked getobject], [direccion getobject], [email getobject], [telefono getobject], nil]; 

This might works