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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10719205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:24:07  来源:igfitidea点击:

How to convert the json string into array of elements?

iosxcodejsonnsmutablearraynsdictionary

提问by kumar

Here is my code to get a json response string

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

I am getting this string by using this method

我通过使用这种方法得到这个字符串

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

To print the "str1"

打印“str1”

I am getting

我正进入(状态

(
    "[email protected]"
),
    (
    "[email protected]",
    "[email protected]"
),
    (
    "[email protected]"
),
    (
    "[email protected]"
),
    (
    "[email protected]"
),
    (
    "[email protected]"
),
    (
    "[email protected]"
),
    (
    "[email protected]"
)

)

)

I am trying to store in array by using "," operator

我正在尝试使用“,”运算符存储在数组中

like this way

像这样

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

but I didn't split the string into array of elements

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

Could please any one help me?

可以请任何人帮助我吗?

采纳答案by Vijay-Apple-Dev.blogspot.com

    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);

回答by cutebug

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