我需要通过POST将这个JSON发送到Web服务:
//array of expanded nodes
expandedNodes = ["analytics", "flare", "graph", "scale"];
function collapse(d) {
if (d.children) {
//if node present in expanded nodes array keep it open.
if (expandedNodes.find(function(d1){ return d1 == d.name})){
//expanded nodes children should be collapsed.
d.children.forEach(collapse);
return;
}
d.children.forEach(function(child){
child.parent = d;
});
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
我使用值创建了对象Tarjeta:
{
"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"
}
]
}
在此之后,我使用此方法进行从对象到JSON的转换:
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;
当我完成此过程时,我将NSMutableDictionary(如JSON)发送到Web服务,但它说这是错误的,这是错误。
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;
我做错了什么?
答案 0 :(得分:2)
将此方法添加到ValorPlantilla类
-(NSMutableDictionary *)getobject
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:self.key forKey:@“key"];
[dict setObject:self.value forKey:@“value"];
return dict;
}
并更改此行
tarjeta.valoresPlantilla = [NSArray arrayWithObjects:[nombre getobject], [curso, linked getobject], [direccion getobject], [email getobject], [telefono getobject], nil];
这可能有效
答案 1 :(得分:1)
首先制作六个单独的词典,
{
"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"
}
然后创建一个数组,让我们说temp
数组,将所有六个字典添加到该数组中。
然后在下面的字典中说出tempDic
,
"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"
},
},
它有副词。
然后制作标准字典(final)并设置三个对象:plantilla, tempDic and temp array
因此,您的对象已准备好发送。
现在,不要在json中转换它。在NSdata中转换它,如下所示
如果您的最终字典名称为finalDic
,那么
NSData *data = [NSJSONSerialization dataWithJSONObject:finalDic options:0 error:nil];
并将该数据发送到服务器。
如果您使用的是AFNetworking
,则无需将finalDic
转换为data
。 Afnetworking
自动管理,只需将finalDict
作为AFNetworking
的网络服务调用参数传递。
希望这会有所帮助:)
答案 2 :(得分:0)
请检查以下网址错误的json数据:
{
"plantilla" : "IL3\/il3_prof",
"contacto" : {
"email" : "vgonzalez@ub.edu",
"nombre" : "Vanesa",
"movilPersonal" : "934037680",
"apellidos" : "Gonzalez Jimenez.", //Please separate ','
"direccionTrabajoDTO" : {
"direccion" : "Feixa Llarga, S\/N 08970 Hospitalet De Llobregat (Barcelona), España"
} // Please remove separete
},
"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"
}
]
}