【问题标题】:Incorrect conversion from object to JSON?从对象到 JSON 的转换不正确?
【发布时间】:2016-05-27 06:27:44
【问题描述】:

我需要通过 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)发送到 Web 服务,但它显示不正确,这就是错误。

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

我做错了什么?

【问题讨论】:

  • 请分享您的错误
  • 'tarjeta.valoresPlantilla = [NSArray arrayWithObjects:[nombre toJSON], [curso toJSON], [linked toJSON], [direccion toJSON], [email toJSON], [telefono toJSON], nil]; '这里不需要在json中转换,只需在此处添加对象即可。
  • 好吧,我已经试过了,但它说错误。 @Shreyank

标签: ios objective-c json object nsdictionary


【解决方案1】:

将此方法添加到您的 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]; 

这可能有效

【讨论】:

  • 谢谢...@Lion
【解决方案2】:

先制作以下六个单独的字典,

 {
  "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 转换为dataAfnetworking 自动管理,只需将 finalDict 作为 AFNetworking 调用的 webservice 参数传递。

希望这会有所帮助:)

【讨论】:

    【解决方案3】:

    请检查您的 json 数据是否有误:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 2023-04-03
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      相关资源
      最近更新 更多