views:

108

answers:

1

Having a little trouble processing my NSStrings into a valid JSON string.

NSString *version       = @"1.1";
NSString *callMethod    = @"auth.login";
NSString *paramsConfig  = [NSString stringWithFormat:@"{\"email\":\"%@\",\"password\":\"%@\"}", usernameString, passwordString];

int queryId     = arc4random()% 10000000;

NSDictionary *userData          = [NSDictionary dictionaryWithObjectsAndKeys:version, @"version", callMethod, @"method", [NSNumber numberWithInt:queryId], @"id", paramsConfig, @"params", nil];
NSString* jsonString            = [userData JSONRepresentation];

Expected JSON string:

{"version":"1.1","params":"{"email":"s","password":"s"}","id":12345678,"method":"auth.login"}

Actual JSON string:

{"version":"1.1","params":"{\"email\":\"s\",\"password\":\"s\"}","id":12345678,"method":"auth.login"}

Not really sure where I'm going wrong. Any thoughts?

Thanks

Sam

+1  A: 

The paramsConfig part of your JSON is a string, and will be escaped as such. IIRC, if you change paramsConfig to be a NSDictionary, then set the values for email and password there, the correct JSONRepresentation will be outputted.

Mr. Matt
Spot on!Thank you very much works like a charm.
Sam