I want to convert personArray
to a JSON string, and send a request to the server.
I tried something like the following code:
@interface Person : NSObject {
NSString *name;
int registered;
}
+ (NSMutableArray *) select;
NSMutableArray *personArray = [Person select];
NSString *json = @"{ \"";//TODO
for (int i =0 ;i < [personArray count]; i++) {
Person *temp = [Person objectAtIndex:i];
[json stringByAppendingFormat:[NSString stringWithFormat:@"\"name\": \"%@\"", temp.name]
}
json = [json stringByAppendingFormat:[NSString stringWithFormat:@"} \""]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[global userID] forKey:@"user_id"];
[request setPostValue:json forKey:@"json_key"];
[request addRequestHeader:@"Content-type" value:@"application/json"];
[request startSynchronous];
The server receives the following data:
{ \"\"name\": \"Tom\"}
The server code is this:
$json = $_POST['json_key'];
echo $json;
$json = json_decode($json, true);
echo $json; // prints nothing
Is there any way to remove the slash, or a prettier solution for converting the object to JSON?