我有一个名为catobras的实体类,它继承自nsmanagedobject类。我想做的是通过json格式的web服务(post)传递它。如果我发送对象,我将收到:

<catObras: 0x6d879f0> (entity: catObras; id: 0x6d859b0 <x-coredata://41B60B06-248C-488D-A14C-894E04D0395F/catObras/p2> ; data: {
Reporte = "<relationship fault: 0x6d9dc40 'Reporte'>";
calendarioVisitas = "<relationship fault: 0x6dac2c0 'calendarioVisitas'>";
catFondosInversion = "<relationship fault: 0x6d7a1b0 'catFondosInversion'>";
contratista = nil;
contrato = "XX-AYTO-ENS-BC-HABITAT-2011-IS-01";
descripcion = "AMPLIACION DE RED DE DRENAJE SANITARIO COL. VISTA HERMOSA";
direccion = nil;
estatus = nil;
fechaAprobacion = "2012-01-01 08:00:00 +0000";
fechaInicio = "2012-01-31 08:00:00 +0000";
fechaTerminacion = "2012-03-10 08:00:00 +0000";
fechaUltimoRep = nil;
idAgrupacion = 0;
idObra = 5;
inversionAprobada = 0;
inversionAutorizada = 1668000;
inversionContratada = 834000;
nota = asdasdasdasda;
numeroControl = "";
obraPartidas = "<relationship fault: 0x6d7a1f0 'obraPartidas'>";
observaciones = "muestras 2";
oficioAprobacion = "SDS/122/11/1857 OF.001";
supervisor = nil;})

我想用json格式传递这些信息。现在我正在使用sbjson(也称为json框架)。当我尝试使用jsonrepresentation时,它会发送给我:
jsonrepresentation失败。错误是:JSON的类型无效
有什么建议吗?

最佳答案

我不熟悉sbjson,但我的猜测是-JSONRepresentation应该在nsdictionary上调用,而不是任意对象。你可以这样做:

NSManagedObject *managedObject = ...;
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", ..., nil]; // These are the keys for the properties of your managed object that you want in the JSON
NSString *json = [[managedObject dictionaryWithValuesForKeys:keys] JSONRepresentation];

-dictionaryWithValuesForKeys:方法在documentation中描述。重要的是,它返回一个NScript字典,SBJSON可以处理它。您需要小心nsmanagedobject子类中每个属性的类型,确保它们是sbjson可以处理的类型。

09-09 19:49