iOS 字典和json之间的转化
来源:互联网 发布:linux 解压到根目录 编辑:程序博客网 时间:2024/05/17 02:01
/**
* 字典转为json
*
* @param dcit 字典类型数据
*
* @return json字符串
*/
- (NSString *)dictToJson:(NSDictionary *)userInf
{
// isValidJSONObject判断对象是否可以构建json对象
if (![NSJSONSerialization isValidJSONObject:userInf]) {
return nil;
}
NSError *error;
// 创造一个json从Data, NSJSONWritingPrettyPrinted指定的JSON数据产的空白,使输出更具可读性。
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInf options:NSJSONWritingPrettyPrinted error:&error];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return json;
}
/**
* json字符串转为字典
*
* @param json json字符串
*
* @return 字典类型数据
*/
- (NSDictionary*) jsonToDcit:(NSString*)json
{
if (!json || json.length == 0) {
return nil;
}
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"jsonToDcit failed: %@", error.description);
}
return dict;
}
JSONObjectWithData:options:error:方法来进行数据转换,这里的options是一个枚举值
即:
typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),
NSJSONReadingAllowFragments = (1UL << 2)
} NS_ENUM_AVAILABLE(10_7, 5_0)
NSJSONReadingMutableContainers:返回可变容器,NSMutableDictionary或NSMutableArray。NSJSONReadingMutableLeaves:返回的JSON对象中字符串的值为NSMutableString
NSJSONReadingAllowFragments:允许JSON字符串最外层既不是NSArray也不是NSDictionary,
但必须是有效的JSON Fragment。例如使用这个选项可以解析 @“123” 这样的字符串,例如:
NSString *num=@"32"; NSError *error; NSData *createdData = [num dataUsingEncoding:NSUTF8StringEncoding]; id response=[NSJSONSerialization JSONObjectWithData:createdData options:NSJSONReadingAllowFragments error:&error]; NSLog(@"Response= %@",response);
/**
* json data 转为字典
*
* @param json json data
*
* @return 字典类型数据
*/
- (NSDictionary*) jsonDataToDcit:(NSData *)data
{
if (!data) {
return nil;
}
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"jsonDataToDcit failed: %@", error.description);
}
return dict;
}
- iOS 字典和json之间的转化
- json和javascript之间的转化
- Json 和复杂 Bean 之间的转化
- json串转化IOS字典过程中遇到的问题
- ios NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化
- json对象和json字符串之间的转化
- 字典、数组和json字符串的相互转化
- iOS ~ json、字典和对象的区别
- json串之间的转化
- JavaScript中json对象和string对象之间的转化
- NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化
- js中的json对象和字符串之间的转化
- js中的json对象和字符串之间的转化
- NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化
- JavaScript中json对象和string对象之间的转化
- js中的json对象和字符串之间的转化
- JSON数据与NSDictionary和NSArray之间的转化
- NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化
- Java程序员常犯的10项错误
- Java中过滤出字母、数字和中文的正则表达式
- ORACLE同义词总结
- mysql 日期函数相关 及一个小sql写法
- 高版本系统安装Oracle 11g 提示 NS-13001
- iOS 字典和json之间的转化
- Android源代码结构分析
- Android 图片裁剪功能实现详解(类似QQ自定义头像裁剪)
- 【SDOI2015】【BZOJ3990】排序
- mysql mongodb PostgreSQL运用技能
- java 注解 详解
- ECShop出现Strict Standards: Only variables should be passed by reference in的解决方法
- CSS设置渐变背景色
- Android中Handler Runnable与Thread的区别详解