开发者

Trouble correctly formatting NSString for JSON

Having a little trouble processing my NSStrings in开发者_如何转开发to 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜