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

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

具体 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. J2EE课程设计:在线书店管理系统

    1.系统实现 使用SpringMVC框架进行开发 使用Maven进行系统构建 使用MySql数据库 项目只实现了查看图书,搜索图书,加入购物车,创建订单,图书管理等基本功能 前台使用Bootstrap ...

  2. python学习(四)--POST请求

    from urllib import requestimport urllibimport re #网页版有道翻译是ajax的post请求. #浏览器请求地址http://fanyi.youdao.c ...

  3. js之方法

    原文 在一个对象中绑定函数,称为这个对象的方法. 在JavaScript中,对象的定义是这样的: var xiaoming = { name: '小明', birth: 1990 }; 但是,如果我们 ...

  4. final关键字介绍

    许多程序设计语言都有自己的办法告诉编译器某个数据是“常数”.常数主要应用于下述两个方面: (1) 编译期常数,它永远不会改变 (2) 在运行期初始化的一个值,我们不希望它发生变化 对于编译期的常数,编 ...

  5. HTML表单(form)的“enctype”属性

    Form元素的语法中,EncType表明提交数据的格式 属性值: application/x-www-form-urlencoded:在发送前编码所有字符(默认) multipart/form-dat ...

  6. javascript对象(3)

    这个对象,不是那个对象,第三哦! 对象之间会存在继承,所以,来说一下他们之间存在的三种三种继承方式: 1.冒用继承 //创建了孙悟空构造函数 function Sun(change,weapon,gf ...

  7. 30 Excellent WordPress Video Tutorials

    http://sixrevisions.com/wordpress/30-excellent-wordpress-video-tutorials/ WordPress是一种使用PHP语言开发的博客平台 ...

  8. Android上使用RecyclerView实现顶部悬浮标题效果的Sticky Title View

    目前很多的项目都在使用顶部悬浮标题的效果,很明显,这的确是一个比较人性化,用户体验效果比较好的UI交互效果,对于这个效果,有很多种实现方式,如果说要用RecyclerView来实现一个分类信息展示,并 ...

  9. sql server中将自增长列归零

    一个项目完成后数据库中会有很多无用的测试数据,可以使用delete * 将数据全部删除,但自增长列(一般是主键)基数不会归零,使用TRUNCATE函数可以将表中数据全部删除,并且将自增长列基数归零.一 ...

  10. 进程状态转换及其PCB的变化

    代码实现了模拟进程状态转换及其相应PCB内容.组织结构的变化. #include<iostream> using namespace std; typedef struct pcb{ in ...