0

我有字符串

测试字符串 =====>{"Name":"0","Age":"0","Sex":"0","weight":"0"}

想要将此字符串转换为这种类型的数组:

myArray[0]="Name";
myArray[1]="Age";
myArray[2]="Sex";
myArray[3]="weight";

请帮助解决这个问题..提前致谢。

4

3 回答 3

2

尝试

    NSArray *arrComponents = [str componentsSeparatedByString:@","];
    NSMutableArray *arrmFilter = [[NSMutableArray alloc] init];

    for(int i = 0; i < [arrComponents count] ;i++)
    {
        NSString *str = [arrComponents objectAtIndex:i];
        str = [str stringByReplacingOccurrencesOfString:@":" withString:@""];
        str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        str = [str stringByReplacingOccurrencesOfString:@"{" withString:@""];
        str = [str stringByReplacingOccurrencesOfString:@"}" withString:@""];
        str = [str stringByReplacingOccurrencesOfString:@"0" withString:@""];

        [arrmFilter addObject:str];
    }

    NSLog(@"Filtered array = %@", arrmFilter);
于 2013-05-13T09:39:36.607 回答
1

这个怎么样?

NSArray *yourNewArray = [testString componentsSeparatedByString:","]; 

我不是 100% 确定您的测试字符串,但是此方法会NSArray根据您在字符串中的特定分隔符返回一个对象。有关更多详细信息,请参阅Apple 文档

于 2013-05-13T09:21:41.103 回答
1

尝试这个

// 下面的代码返回包含 4 个字符串对象但字符包括 {,},:,0 的数组

 NSArray *arr=[str componentsSeparatedByString:@","];
 NSMutableArray *arrNew = [[NSMutableArray alloc] init];

//从所有数组对象中删除那些不寻常的字符

for(int i = 0;i <[arr count] ;i++)
   {
      NSString *str = [arrComponents objectAtIndex:i];
    str = [str stringByReplacingOccurrencesOfString:@":" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"{" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"}" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"0" withString:@""];

    [arrNew addObject:str];
   }
于 2013-05-13T09:27:05.670 回答