1、将NSData转换成Dictionary
/** 将二进制数据转换成字典*/
+ (NSDictionary *)dictionaryForJsonData:(NSData *)jsonData
{
if (![jsonData isKindOfClass:[NSData class]] || jsonData.length < 1) {
return nil;
}
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
if (![jsonObj isKindOfClass:[NSDictionary class]]) {
return nil;
}
return [NSDictionary dictionaryWithDictionary:(NSDictionary *)jsonObj];
}
2、将Dictionary转为NSData
/** 将字典转换成json格式字符串,不含 这些符号*/
+ (NSData *)compactFormatDataForDictionary:(NSDictionary *)dicJson
{
if (![dicJson isKindOfClass:[NSDictionary class]]) {
return nil;
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
if (![jsonData isKindOfClass:[NSData class]]) {
return nil;
}
return jsonData;
}