开发者

Trouble correctly formatting NSString for JSON

开发者 https://www.devze.com 2022-12-30 02:06 出处:网络
Having a little trouble processing my NSStrings in开发者_如何转开发to a valid JSON string. NSString *version= @\"1.1\";

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

精彩评论

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

关注公众号