pasar online example descargar data convertir convert application iphone objective-c json sbjson

iphone - online - crear una cadena json desde NSArray



pasar whatsapp de android a iphone 2018 (6)

Aunque la respuesta más votada es válida para un conjunto de diccionarios u otros objetos serializables, no es válida para objetos personalizados.

Aquí está la cosa, tendrá que recorrer su matriz y obtener la representación del diccionario de cada objeto y agregarla a una nueva matriz para ser serializada.

NSString *offersJSONString = @""; if(offers) { NSMutableArray *offersJSONArray = [NSMutableArray array]; for (Offer *offer in offers) { [offersJSONArray addObject:[offer dictionaryRepresentation]]; } NSData *offersJSONData = [NSJSONSerialization dataWithJSONObject:offersJSONArray options:NSJSONWritingPrettyPrinted error:&error]; offersJSONString = [[NSString alloc] initWithData:offersJSONData encoding:NSUTF8StringEncoding] ; }

En cuanto al método dictionaryRepresentation en la clase Offer:

- (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:self.title forKey:@"title"]; return [NSDictionary dictionaryWithDictionary:mutableDict]; }

En mi aplicación de iPhone tengo una lista de objetos personalizados. Necesito crear una cadena json de ellos. ¿Cómo puedo implementar esto con SBJSON o iPhone SDK?

NSArray* eventsForUpload = [app.dataService.coreDataHelper fetchInstancesOf:@"Event" where:@"isForUpload" is:[NSNumber numberWithBool:YES]]; SBJsonWriter *writer = [[SBJsonWriter alloc] init]; NSString *actionLinksStr = [writer stringWithObject:eventsForUpload];

y me sale el resultado vacío.


Este proceso es realmente simple ahora, no tiene que usar bibliotecas externas, hágalo de esta manera (iOS 5 y superior)

NSArray *myArray; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArray options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


Llego un poco tarde a esta fiesta, pero puede serializar una matriz de objetos personalizados implementando el método -proxyForJson en sus objetos personalizados. (O en una categoría en sus objetos personalizados.)

Por un example


Me encantan mis categorías, así que hago este tipo de cosas de la siguiente manera

@implementation NSArray (Extensions) - (NSString*)json { NSString* json = nil; NSError* error = nil; NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error]; json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return (error ? nil : json); } @end


Prueba así Swift 2.3

let consArray = [1,2,3,4,5,6] var jsonString : String = "" do { if let postData : NSData = try NSJSONSerialization.dataWithJSONObject(consArray, options: NSJSONWritingOptions.PrettyPrinted) { jsonString = NSString(data: postData, encoding: NSUTF8StringEncoding)! as String } } catch { print(error) }


Prueba asi

- (NSString *)JSONRepresentation { SBJsonWriter *jsonWriter = [SBJsonWriter new]; NSString *json = [jsonWriter stringWithObject:self]; if (!json) [jsonWriter release]; return json; }

entonces llama a esto como

NSString *jsonString = [array JSONRepresentation];

Espero que te ayude ...