如何将json字符串转换为元素数组?

时间:2012-05-23 11:42:33

标签: ios xcode json nsmutablearray nsdictionary

这是获取json响应字符串的代码

我使用此方法获取此字符串

NSDictionary *selector = [json valueForKey: @"Title"];
NSMutableArray *dictArray = [[NSMutableArray alloc] initWithObjects: selector,nil];
str1=[[dictArray objectAtIndex:i] valueForKey:@"CompanyName"];

打印“str1”

我正在

(
    "hello@developer.com"
),
    (
    "hello@developer.com",
    "helloworld@microsoft.com"
),
    (
    "hello@developer.com"
),
    (
    "hello@developer.com"
),
    (
    "hello@developer.com"
),
    (
    "hello@developer.com"
),
    (
    "hello@developer.com"
),
    (
    "hello@developer.com"
)

我试图使用“,”运算符

存储在数组中 像这样

NSArray *SplitStringArray = [str1 componentsSeparatedByString:@", "];

但我没有将字符串拆分为元素数组

可以请任何人帮助我吗?

2 个答案:

答案 0 :(得分:1)

    NSDictionary *selector = [json valueForKey: @"Title"];

    NSMutableArray *dictArray = [[NSMutableArray alloc] initWithObjects: selector,nil];

    NSMutableArray *str1Array=[[dictArray objectAtIndex:i] valueForKey:@"CompanyName"];


        //note this line
    NSMutableArray *SplitStringArray =[[NSMutableArray alloc]init];

    for (id eachArray in str1Array) {


        if ([eachArray count]>0) {


            for (int i = 0; i<[eachArray count]; i++) {

                NSString *emailid=[NSString stringWithFormat:@"%@",[eachArray objectAtIndex:i]];

                [SplitStringArray addObject:emailid];


            }

        }

    }

    NSLog(@"SplitStringArray : %@",SplitStringArray);

答案 1 :(得分:1)

NSMutableArray *SplitStringArray = [[NSMutableArray alloc]init]; 
[SplitStringArray setArray:[str1 componentsSeparatedByString:@","]];