如何删除& quot;在json字符串?

时间:2014-12-15 08:01:01

标签: ios objective-c iphone nsstring

我被打了一个模块超过一周,我有一个登录页面,它包含用户名和密码,我需要将我的值传递给Web服务以检查登录。我的Web服务只包含一个名为“Param”的参数名称,我需要将值传递给“Param”。但是,当我输入有效的用户名和密码时,在调用Web服务时没有问题,这是因为当我将我的值传递给登录页面时添加了& quot 附加文本

这里我的输出打印在Nslog中:

{"login_values":{"\"password":"xxxxx@1234","\"loginid":"xxxxxxx@gmail.com"}}

我的示例代码:

    NSString *login = self.username.text;
    NSString *password = self.password.text;
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setValue:login forKey:@"loginid"];
    [dict setValue:password forKey:@"password"];
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *resultAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"jsonData as string:\n%@", resultAsString);
    NSData *jsonData1 = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
    NSString *addnewuser_values = [[NSString alloc]initWithData:jsonData1 encoding:NSUTF8StringEncoding];
    NSString *append = [NSString stringWithFormat:@"{\"login_values\":"];
    NSString *slash = [NSString stringWithFormat:@"}"];
    NSString *append1 = [NSString stringWithFormat:@"%@%@%@",append,addnewuser_values,slash];
   NSString *modStr=[[append1 stringByReplacingOccurrencesOfString:@""" withString:@""]stringByReplacingOccurrencesOfString:@"," withString:@" "];
    NSLog(@"locginCheck_method:\n%@",append);
    request.param = modStr;

我需要删除该& quot;

2 个答案:

答案 0 :(得分:0)

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

检查这是否有助于你

答案 1 :(得分:0)

使用stringByReplacingOccurrencesOfString api,如下所示: -

NSString *modStr=[[jsonStr stringByReplacingOccurrencesOfString:@""" withString:@""]stringByReplacingOccurrencesOfString:@"," withString:@" "];

输出: -

{login_values:{password:xxxxx@1234 loginid:xxxxxxx@gmail.com}}