日常开发中常用的一个相互转换的方法;

直接创建对应的类,引用如下方法即可实现;

具体 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的更多相关文章

  1. NSString+JSON - iOS

    日常开发中常用的一个相互转换的方法; 直接创建对应的类,引用如下方法即可实现; 具体 code 如下: 声明: #import <Foundation/Foundation.h> @int ...

  2. iOS NSDictionary JSON 相互转换

    /*! * @brief 把格式化的JSON格式的字符串转换成字典 * @param jsonString JSON格式的字符串 * @return 返回字典 */ + (NSDictionary * ...

  3. JSON TO NSDictionary Mac & iOS

    NSString * jsonPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Cont ...

  4. NSDictionary json格式字符串转字典,字典转json格式字符串

    + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return ...

  5. NSDictionary 总结 -iOS

    总结:字典分NSDictionary(不可变,只能查询)和NSMutableDictionary(可变.能增删改查)两种,形式是key-value,key是不可重复的,value可以重复 1.初始化字 ...

  6. iOS NSDictionary、NSData、JSON数据类型相互转换

    iOS经常需要用到数据类型的转换,下面列举一下常用类型的转换. 1.NSDictionary类型转换为NSData类型: //NSDictionary -> NSData: NSDictiona ...

  7. iOS NSDictionary、NSData、JSON等 数据类型相互转换

    1.NSDictionary类型转换为NSData类型: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @&qu ...

  8. iOS开发网络篇—发送json数据给服务器以及多值参数

    iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...

  9. iOS Json转换模型库:YYModel

    iOS Json转换模型库:YYModel   其实在研究这个库之前,市面上已经有很多类似的模型序列化成JSON及反序列化库(如Mantle.MJExtension)了,推荐他只是因为他高端的性能和容 ...

随机推荐

  1. 线程操作之Thread类

    在.Net fremework 中,所有与线程有关的操作都封装在System.Thread命名空间中, 所以在操作线程时,要先引入次命名空间 Thread类的常用方法 Abort 线程终止 Join ...

  2. 在windows上用netsh动态配置端口转发

    使用多个虚拟机,将开发环境和工作沟通环境分开(即时通,办公系统都只能在windows下使用…),将开发环境的服务提供给外部访问时,需要在主机上通过代理配置数据转发. VirtualBox提供了端口转发 ...

  3. C# Attribute应用:类签名

    在应用别人接口的时候,总是要用签名,很是不理解签名这是怎么知道做的.通过对Attribute的学习了解.大体可以用Attribute来做签名应用. 具体过程如下: 首先我们要先定义一个类,该类继承At ...

  4. mysql五补充:SQL逻辑查询语句执行顺序(待完善)

    一.SELECT语句关键字的定义顺序(语法顺序) SELECT DISTINCT <select_list> FROM <left_table> <join_type&g ...

  5. css3 border-image及连续的图像边框

    border-image 它是下面几个值的简写: border-image-source // 使用绝对或相对地址url,引入图片 border-image-slice   //切割图片,取值支持:& ...

  6. unity导出apk错误出错解决方法

    CommandInvokationFailure: Failed to re-package resources. See the Console for details. F:\adt-bundle ...

  7. Spyder更改默认工作路径已经文件路径

    打开spyder,选择菜单栏中的Tools--->Preferences--->Current working directory   然后选择最下面的单选按钮The following ...

  8. How to reference two table when lack reference column.

    Question:How to reference two table when lack reference column. Example: 1.Create two tables the one ...

  9. 插上翅膀,让Excel飞起来——xlwings(一)

    python操作Excel的模块,网上提到的模块大致有:xlwings.xlrd.xlwt.openpyxl.pyxll等,他们提供的功能归纳起来有两种:一.用python读写Excel文件,实际上就 ...

  10. python:类与对象命名空间、面对对象的组合用法

    1,类里可以定义两种属性: #静态属性 #静态属性就是直接在类中定义的变量 #动态属性 #动态属性就是定义在类中的方法 class Course: language = ['Chinese']#静态属 ...