json字符串和字典、数组之间互转

#pragma mark - dic/array转为jsonString(NSJSONSerialization解析)
/**
 *  @author zm
 *
 *  @brief  dic/array转为jsonString(NSJSONSerialization解析)
 *
 *  @param theData dic/array
 *
 *  @return jsonString
 */
- (NSString *)f_toJSONString:(id)theData
{
    NSString *_str_json =nil;
    if (theData) {
        if ([theDataisKindOfClass:[NSDictionaryclass]] || [theData isKindOfClass:[NSMutableDictionaryclass]] || [theData isKindOfClass:[NSArrayclass]] || [theData isKindOfClass:[NSMutableArrayclass]]) {
            NSData *_data_json = [NSJSONSerializationdataWithJSONObject:theData
                                                              options:NSJSONWritingPrettyPrinted
                                                                error:nil];
            if (_data_json) {
                _str_json = [[NSStringalloc] initWithData:_data_json
                                                encoding:NSUTF8StringEncoding];
            }
        }
    }
    return _str_json;
}

#pragma mark jsonString转为NSMutableDictionary(NSJSONSerialization解析)
/**
 *  @author zm
 *
 *  @brief  jsonString转为NSMutableDictionary(NSJSONSerialization解析)
 *
 *  @param theData dic/array
 *
 *  @return jsonString
 */
- (NSMutableDictionary *)f_jsonDataToDic:(NSData*)jsonData option:(NSJSONReadingOptions)option
{
    NSError *_err_temp;
    if(jsonData)
    {
        NSMutableDictionary *_muDic_json = [NSJSONSerialization
                                          JSONObjectWithData:jsonData
                                          options:option error:&_err_temp];
        if(_muDic_json && _muDic_json.count >0)
        {
            return _muDic_json;
        }
    }
    return nil;
}


你可能感兴趣的:(json字符串转化)