NSJSONSerialization介绍

前端之家收集整理的这篇文章主要介绍了NSJSONSerialization介绍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。



@H_502_12@//将二进制数据转化为字典 +(NSDictionary *)jsonWithData:(NSData *)data { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; return dict; } // 将字典或者数组转化为JSON串 - (NSData *)toJSONData:(id)theData{ NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error:&error]; if ([jsonData length] > 0 && error == nil){ return jsonData; }else{ return nil; } }

猜你在找的Json相关文章