NSManagedObject到JSON [英] NSManagedObject to JSON

查看:106
本文介绍了NSManagedObject到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为catObras的实体类,它从类NSManagedObject继承.我想要做的是通过JSON形式通过Web服务(POST)传递它.如果我发送了对象,则会收到以下消息:

I have an entity class called catObras, and it inherits from the class NSManagedObject. What I want to do is pass it trough a web service (POST) in JSON form. If I sent the object I receive this:

<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时,它向我发送了以下消息:

I would like to pass this information in json form. Right now I'm using SBJSON (aka JSON-framework). When I try to use JSONRepresentation, it sends me this:

JSONRepresentation失败.错误是:JSON的无效类型

JSONRepresentation failed. Error is: Not valid type for JSON

有什么建议吗?

推荐答案

我对SBJSON不熟悉,但是我猜想-JSONRepresentation应该在NSDictionaries上调用,而不是任意对象.您可以这样做:

I'm not familiar with SBJSON, but my guess is that -JSONRepresentation is meant to be called on NSDictionaries, not arbitrary objects. You could do this:

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:方法在

The -dictionaryWithValuesForKeys: method is described in the documentation. Importantly it returns an NSDictionary, which SBJSON can probably handle. You'll need to be careful with the types of each attribute on your NSManagedObject subclass, being sure that they are of a type that SBJSON can handle.

这篇关于NSManagedObject到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆