从对象到 JSON 的转换不正确?

Incorrect conversion from object to JSON?

我需要通过 POST 将此 JSON 发送到 Web 服务:

{
  "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"
    }
  ]
}

我创建了对象 Tarjeta,其值为:

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;

在此之后,我使用以下方法将对象转换为 JSON:

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;

当我完成这个过程后,我将 NSMutableDictionary 像 JSON 一样发送到网络服务,但它说这是不正确的,这就是错误。

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

我做错了什么?

请检查您的 json 下面的数据有误 url:

http://jsoneditoronline.org/

{
"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"
 }
]
}

先制作下面六个人的字典,

 {
  "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"
},
},

它有子词典。

然后制作标准字典(最终)并设置三个对象:plantilla, tempDic and temp array

因此,您的对象已准备好发送。

现在,不要在json中转换它。像下面这样在 NSdata 中转换它

如果你最终的词典名称是 finalDic 那么,

   NSData *data = [NSJSONSerialization dataWithJSONObject:finalDic options:0 error:nil];

并将该数据发送到服务器。

如果您使用的是 AFNetworking,则无需将 finalDic 转换为 dataAfnetworking 自动管理,只需将 finalDict 作为参数传递给 AFNetworking

希望这会有所帮助:)

将此方法添加到您的 ValorPlantilla class

-(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]; 

这可能有效