NSDictionary+JSON - iOS
日常开发中常用的一个相互转换的方法;
直接创建对应的类,引用如下方法即可实现;
具体 code 如下:
声明:
#import <Foundation/Foundation.h> @interface NSDictionary (JSON) - (NSString *)jsonString; - (void)writeToJsonFile:(NSString *)path atomically:(BOOL)atomically; + (NSDictionary *)dictionaryWithContentsOfJsonFile:(NSString *)path; @end
实现:
#import "NSDictionary+JSON.h"
#import "NSString+JSON.h" @implementation NSDictionary (JSON) - (NSString *)jsonString {
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
options:0
error:&error];
if (error)
return nil;
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonString;
} - (void)writeToJsonFile:(NSString *)path atomically:(BOOL)atomically {
NSData *content = [[self jsonString] dataUsingEncoding:NSUTF8StringEncoding];
[content writeToFile:path atomically:atomically];
} + (NSDictionary *)dictionaryWithContentsOfJsonFile:(NSString *)path {
NSError *error = nil;
NSString *content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
return nil;
}
id result = [content jsonObject];
if (![result isKindOfClass:[self class]]) {
return nil;
}
return result;
} @end
注:实现类中需要引入的类在如下飞机票中,具体详情请跳转查看.
NSString+JSON - iOS 机票如下: NSString+JSON - iOS
以上便是此次分享的内容,希望能对大家有所帮助!
NSDictionary+JSON - iOS的更多相关文章
- NSString+JSON - iOS
日常开发中常用的一个相互转换的方法; 直接创建对应的类,引用如下方法即可实现; 具体 code 如下: 声明: #import <Foundation/Foundation.h> @int ...
- iOS NSDictionary JSON 相互转换
/*! * @brief 把格式化的JSON格式的字符串转换成字典 * @param jsonString JSON格式的字符串 * @return 返回字典 */ + (NSDictionary * ...
- JSON TO NSDictionary Mac & iOS
NSString * jsonPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Cont ...
- NSDictionary json格式字符串转字典,字典转json格式字符串
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return ...
- NSDictionary 总结 -iOS
总结:字典分NSDictionary(不可变,只能查询)和NSMutableDictionary(可变.能增删改查)两种,形式是key-value,key是不可重复的,value可以重复 1.初始化字 ...
- iOS NSDictionary、NSData、JSON数据类型相互转换
iOS经常需要用到数据类型的转换,下面列举一下常用类型的转换. 1.NSDictionary类型转换为NSData类型: //NSDictionary -> NSData: NSDictiona ...
- iOS NSDictionary、NSData、JSON等 数据类型相互转换
1.NSDictionary类型转换为NSData类型: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @&qu ...
- iOS开发网络篇—发送json数据给服务器以及多值参数
iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...
- iOS Json转换模型库:YYModel
iOS Json转换模型库:YYModel 其实在研究这个库之前,市面上已经有很多类似的模型序列化成JSON及反序列化库(如Mantle.MJExtension)了,推荐他只是因为他高端的性能和容 ...
随机推荐
- docker-compose 安装solr+ikanalyzer
docker-compose.yml version: '3.1' services: solr: image: solr restart: always container_name: solr p ...
- 4、Angular2 pipe
1. stateless pipe 2.stateful pipe
- package.json中dependencies 和devDependencies的差异
我们在日常开发中,经常会使用到npm安装对应的包,会经常在package.json中看到dependencies 和devDependencies 二者的区别: devDependencies:是你开 ...
- jeecg308 <t:authFilter />标签失效的问题
<%--该标签放到body末尾会无效,估计是js冲突,放到body前好用--%><t:authFilter /> <body></body>
- My eclipse jdk unbound的解决
project --> properties --> java build path --> 双击出错的jdk --> alternate jre --> install ...
- <转>MapReduce工作原理图文详解
转自 http://weixiaolu.iteye.com/blog/1474172前言: 前段时间我们云计算团队一起学习了hadoop相关的知识,大家都积极地做了.学了很多东西,收获颇丰.可是开学 ...
- linux c 监控目录
static void* thread_monitor(void* args) { pthread_detach(pthread_self()); int fd; int wd; int len; i ...
- python 按值排序
转自:http://www.cnpythoner.com/post/266.html,感谢分享! python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),如果我们需 ...
- String_Helper
#region 扩展验证方法 #region <<IsNullOrEmpty()字符串是否为空>> /// <summary> /// <para>代码 ...
- gamemakerstudio:room_speed和image_speed
room_speed是游戏步数,每秒多少步(步事件)image_speed是动画帧率room_speed变则整个游戏变慢image_speed变只是该object动画变慢 除了游戏全局加速减速,一般不 ...