iOS如何将数组转换成
1个回答
展开全部
IOS数组可以转换车json对象,转换代码:
NSString *str = nil;
//字符串
NSMutableString *string = [[NSMutableString alloc] init];
[string appendString:@"xxxx"];
str = [string JSONString];
NSLog(@"str1:%@",[NSString stringWithString:str]);
//数组
NSArray *array = [[NSArray alloc] initWithObjects:@"111",@"333",@"222", nil];
// NSMutableArray *array = [NSMutableArray array];
str = [array JSONString];
NSLog(@"str2:%@",[NSString stringWithString:str]);
[array release ];
//字典
NSArray *firstArr = [NSArray arrayWithObjects:@"first",@"second", nil];
//基本数据类型转换成NSNumber类型
NSArray *secondArr = [NSArray arrayWithObjects:[NSNumber numberWithDouble:2.1],[NSNumber numberWithBool:NO], nil];
//加到字典中
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:firstArr,@"first",secondArr,@"second", nil];
//转化成json格式
str = [dic JSONString];
NSString *str2 = [NSString stringWithString:str];
NSLog(@"str3:%@",str2);
//编码
NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:array];
// NSLog(@"archive:%@",archive);
NSArray *arr2 = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
NSLog(@"arr2:%@",arr2);
NSString *str = nil;
//字符串
NSMutableString *string = [[NSMutableString alloc] init];
[string appendString:@"xxxx"];
str = [string JSONString];
NSLog(@"str1:%@",[NSString stringWithString:str]);
//数组
NSArray *array = [[NSArray alloc] initWithObjects:@"111",@"333",@"222", nil];
// NSMutableArray *array = [NSMutableArray array];
str = [array JSONString];
NSLog(@"str2:%@",[NSString stringWithString:str]);
[array release ];
//字典
NSArray *firstArr = [NSArray arrayWithObjects:@"first",@"second", nil];
//基本数据类型转换成NSNumber类型
NSArray *secondArr = [NSArray arrayWithObjects:[NSNumber numberWithDouble:2.1],[NSNumber numberWithBool:NO], nil];
//加到字典中
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:firstArr,@"first",secondArr,@"second", nil];
//转化成json格式
str = [dic JSONString];
NSString *str2 = [NSString stringWithString:str];
NSLog(@"str3:%@",str2);
//编码
NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:array];
// NSLog(@"archive:%@",archive);
NSArray *arr2 = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
NSLog(@"arr2:%@",arr2);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询