Convertir NSArray -> JSON -> NSData -> servidor PHP -> representación JSON
Estoy convirtiendo una matriz de NSDictionaries a datos JSON con lo siguiente ...
Crear mis datos ...
<code>NSMutableArray *arrayOfDicts = [[NSMutableArray alloc] init]; for (int i = 0; i < 2; i++) { NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"MySong", @"title", @"MyArtist", @"artist", nil]; [arrayOfDicts addObject:dict]; } NSArray *info = [NSArray arrayWithArray:arrayOfDicts]; NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error]; </code>
luego enviar como asi ...
<code>NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/index.php"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostValue:jsonData forKey:@"songs"]; [request setDelegate:self]; request.tag = TAG_LOAD_SONGS; [request startAsynchronous]; </code>
Esto es enviar el NSData a mi servidor, pero ¿cómo lo uso en php ... Es solo un montón de números aleatorios si lo imprimo desde mi servidor, y he intentado usar json_encode pero no creo que eso sea para datos en bruto ...
Cualquier ayuda sería genial!
EDITAR: Aquí está la respuesta de php ...
<code>echo $_POST['songs']; <5b0a2020 7b0a2020 20202274 69746c65 22203a20 224d7953 6f6e6722 2c0a2020 20202261 72746973 7422203a 20224d79 41727469 7374220a 20207d2c 0a20207b 0a202020 20227469 746c6522 203a2022 4d79536f 6e67222c 0a202020 20226172 74697374 22203a20 224d7941 72746973 74220a20 207d0a5d> </code>
Aquí está la respuesta a NSLoging en Xcode ...
<code>NSLog(@"Info: %@", info); </code>
Información: ({artist = MyArtist; title = MySong;}, {artist = MyArtist; title = MySong;})