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)了,推荐他只是因为他高端的性能和容 ...
随机推荐
- indexOf.substr,substring,charAt的区别
var a = "asdfghjkl" alert(a.substr(1, 3)); // 从下标为1开始,往右数3个长度的数, 显示 sdf; alert(a.s ...
- 一个C#后台调用接口的例子
string url = ConfigurationSettings.AppSettings["resurl"].ToString(); var wc = new WebClien ...
- IDEA中一个工程多个模块(module)分别提交到不同的git服务器
说明:本文档适用于一个工程多个模块的项目,每个模块对应不同的git服务器地址. 一.将本地项目导入到git服务器 1.打开 File -> Settings,选择 Version C ...
- java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ 解决方案
//第一个异常 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysq ...
- tomcat server 报错之 More than the maximum allowed number of cookies
More than the maximum allowed number of cookies EVERE: Error processing request java.lang.IllegalArg ...
- ThreadPoolExecutor实现原理
转载:https://blog.csdn.net/yanyan19880509/article/details/52718497 前言 做java开发的,一般都避免不了要面对java线程池技术,像to ...
- css border-radius的用法及自适应的椭圆
我们知道border-radius允许您为元素添加圆角边框! 而border-radius 属性是一个简写属性,用于设置四个 border-*-radius 属性. 如果省略 bottom-left, ...
- STROME --realtime & online parallel computing
Data Collections ---> Stream to Channel (as source input) ----> Parallel Computing---> Resu ...
- 【Linux】 Linux编程规范&Linux 编程环境搭建
一.通过Samba映射网络驱动器 菜单栏-计算机-映射网络驱动器 English 菜单栏-Home -Easy access-Map as drive 编辑代码使用 Windows 编译 运行程序在 ...
- js:正则表达式
<script type="text/javascript"> function SubmitCk() { var reg = /^([a-zA-Z0-9]+[_|\_ ...