json数组字符串转换为nsmutablearray

时间:2013-10-24 09:52:34

标签: ios json nsmutablearray

我从服务器获得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

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

2 个答案:

答案 0 :(得分:1)

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

for (NSString *myString in weekDays)
{
    myString = [myString stringByReplacingOccurrencesOfString: @" " withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @"\n" withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @"(" withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @")" withString:@""];
}

答案 1 :(得分: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 );