NString、NSArray、NSDictonry相互转换

/*!  
 * @brief 把格式化的JSON格式的字符串转换成字典  
 * @param jsonString JSON格式的字符串  
 * @return 返回字典  
 */  
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {  
    if (jsonString == nil) {  
        return nil;  
    }  
      
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];  
    NSError *err;  
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData  
                                                        options:NSJSONReadingMutableContainers  
                                                          error:&err];  
    if(err) {  
        NSLog(@"json解析失败:%@",err);  
        return nil;  
    }  
    return dic;  
}  
1. NSString 拆分成 
NSArrayNSString *_string = @"1,2,3,4,5";
NSArray *_arr = [_string componentsSeparatedByString:NSLocalizedString(@",", nil)];
2. NSArray 转为 
NSStringNSArray *_arr = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
NSString *_str = [_arr componentsJoinedByString:@","];

你可能感兴趣的:(NString、NSArray、NSDictonry相互转换)