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?
To be sure that the JSON representation is being generated properly, use a general purpose JSON generator (such as Stig Brautaset's JSON Framewark or yajl-objc) rather than ad hoc conversion.
JSON Framework:
@interface Person(SBJson)
-(id)proxyForJson;
@end
@implementation Person(SBJson)
-(id)proxyForJson {
return [NSDictionary dictionaryWithObjectsAndKeys:
name,@"name",
[NSNumber numberWithInt:registered],@"registered",
nil];
}
@end
...
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[[Person select] JSONRepresentation] forKey:@"json_key"];
yajl-objc:
@interface Person(YAJL)
-(id)JSON;
@end
@implementation Person(YAJL)
-(id)JSON {
return [NSDictionary dictionaryWithObjectsAndKeys:
name,@"name",
[NSNumber numberWithInt:registered],@"registered",
nil];
}
@end
...
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[[Person select] yajl_JSONString] forKey:@"json_key"];
See also:
On the server, you probably have magic_quotes_gpc turned on. Try something more like this:
$json = $_POST['json_key'];
if (get_magic_quotes_gpc()) {
$json = stripslashes($json);
}
echo $json;
$json = json_decode($json, true);
print_r($json);
NSString *json = @"{ \"";//TODO
and
json = [json stringByAppendingFormat:[NSString stringWithFormat:@"} \""]];
Look mighty suspicious to me. You're sticking a quote at the start of your json string, which will lead to the the first key having a stray " in front of it. The " at the end just looks bogus.
Why not use an actual JSON library (e.g. touchjson or sbjson) rather than cobbling together bits of strings ?
I'm surprised this gives you anything at all. For starters, this line…
[json stringByAppendingFormat:[NSString stringWithFormat:@"\"name\": \"%@\"", temp.name]];
… does nothing at all. stringByAppendingFormat:
returns a new string based on the receiver (i.e. json
), it does not modify the receiver. Also, you're creating a new string with a format, to append to json
with a format; it could be simplified to:
json = [json stringByAppendingFormat:@"\"name\": \"%@\"", temp.name];
Also, rather than continuously creating new strings and potentially flooding the autorelease pool, use an NSMutableString
instead, and use the appendFormat:
or appendString:
method which do modify the receiver rather than creating new strings.
As others have mentioned, the reason why the JSON might not be decoded properly is because it may not be well-formed, there is a stray \"
in your JSON string.