0

我从服务器收到 Json 响应。

       type = 2;
      "daysofWeek" = "(\n    Mon,\n    Tue\n)";
       serviceType = 2;
       startDate = "2013-10-28";

在上述格式中,daysofWeek 是数组字符串。我正在尝试将 NSMutableArray 转换为

  NSString *weekDaysStr=[valueDict objectForKey:@"recrWeek_daysofWeek"];
  NSMutableArray *weekDays=[[NSMutableArray alloc] initWithArray:[weekDaysStr componentsSeparatedByString:@","]];

但是当我记录这个数组时,我显示为

     ("\n    Mon",
      "\n    Tue\n"
     )

如何从数组中删除那些多余的单词。

我已经检查了工作日的每个值。

               NSString *day=@"Mon";
               if([day isEqualToString:[weekDays objectAtIndex:0]){

                }

我也试过这种类型

       weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"(\n" withString:@""];                      
       weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\n)" withString:@""];
    weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\n " withString:@""];
     weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@" " withString:@""];                                   
     weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 
     weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@" " withString:@""];

它显示为

        "Mon",
        Tue

当时它给出了错误的条件。帮我解决这个问题

4

2 回答 2

1

您可以尝试自己修剪数组中的字符串:

for (NSString *myString in weekDays)
{
    myString = [myString stringByReplacingOccurrencesOfString: @" " withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @"\n" withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @"(" withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @")" withString:@""];
}
于 2013-10-24T09:54:23.487 回答
1

试试这个它会有所帮助

NSString *str=@"(\n    Mon,\n    Tue\n)";
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"\n)" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"(" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@")" withString:@""];
NSMutableArray *weekDays=[[NSMutableArray alloc] initWithArray:[str componentsSeparatedByString:@","]];
NSLog(@"%@",weekDays );
于 2013-10-24T10:37:58.057 回答