NSArray与NSString、NSData,NSDictionary与NSString、NSData 相互转化
NSArray *array = @[ @1, @2, @3, @4, @5, @3 ];
// 1. NSArray与NSData相互转化
// NSArray to NSData
NSError *error;
NSData *arrayData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
if (!error) {
NSLog(@"arrayData = %@", arrayData);
} else {
NSLog(@"%@", error);
}
// NSData to NSArray
NSArray *data2Array = [NSJSONSerialization JSONObjectWithData:arrayData options:NSJSONReadingMutableLeaves error:&error];
if (!error) {
NSLog(@"data2Array = %@", data2Array);
} else {
NSLog(@"%@", error);
} // 2. NSArray与NSString相互转化
// NSArray to NSString
NSString *arrayStr = [array componentsJoinedByString:@","];
NSLog(@"arrayStr = %@", arrayStr);
// NSString to NSArray
NSArray *string2Array = [arrayStr componentsSeparatedByString:@","];
NSLog(@"string2Array = %@", string2Array); NSDictionary *dictionary = @{ @"name": @"xue zhang", @"sex": @1, @"age": @25 };
// 3. dictionary与NSData相互转化
// NSArray to NSData
NSData *dictionaryData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if (!error) {
NSLog(@"dictionaryData = %@", dictionaryData);
} else {
NSLog(@"%@", error);
}
// NSData to NSDictionary
NSDictionary *data2Dictionary = [NSJSONSerialization JSONObjectWithData:dictionaryData options:NSJSONReadingMutableLeaves error:&error];
if (!error) {
NSLog(@"data2Dictionary = %@", data2Dictionary);
} else {
NSLog(@"%@", error);
} // 4. NSDictionary与NSString相互转化
// NSDictionary to NSString
NSString *dictionaryStr = [[NSString alloc] initWithData:dictionaryData encoding:NSUTF8StringEncoding];
NSLog(@"dictionaryStr = %@", dictionaryStr);
// NSString to NSArray
NSDictionary *string2Dictionary = [NSJSONSerialization JSONObjectWithData:[dictionaryStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"string2Dictionary = %@", string2Dictionary);
NSArray与NSString、NSData,NSDictionary与NSString、NSData 相互转化的更多相关文章
- 为什么有时候NSData转换成NSString的时候返回nil
为什么有时候NSData转换成NSString的时候返回nil 有时候,NSData明明有值,可是,当转换成NSString的时候,却没有值,现在来进行测试:) -现在提供测试用素材- 源码如下: / ...
- JSON Object(如NSDictionary,NSArray)转化为JSON格式的NSString #iOS开发
NSString *string = [self jsonObjectToJSONString:inputDataDic]; -(NSString*)jsonObjectToJSONString:(i ...
- HTTP请求返回的NSData无法转换为NSString
最近在做的一个项目中有一个功能是有一个网页,模拟http请求获取到这个网页返回的相应的数据. 在请求完成后获取到的数据为NSData类型,按照我们通常的转换为NSString的方法: NSString ...
- NSDate与 NSString 、long long类型的相互转化
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3560280.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- NSString *const 和 const NSString * 的区别
1.变量存储的指针可变,变量存储的值不可变 //A modifiable pointer to a constant NSString (its value can't be modified) &q ...
- NSData NSDate NSString NSArray NSDictionary 相互转换
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSDa ...
- NSData NSDate NSString NSArray NSDictionary 相互转化
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; ...
- XML 格式转JSON 格式
#import <Foundation/Foundation.h> #pragma GCC diagnostic push #pragma GCC diagnostic ignored & ...
- runtime实现对象存储型数据库——LHDB
前言 最近在GitHub上看了一份关于基于runtime封装的对象存储型数据库的开源代码,觉得非常值得分享记录一下,在IOS中对数据库的操作一般通过CoreData和SQLite,CoreData 虽 ...
随机推荐
- 转:vue+canvas如何实现b站萌系登录界面
https://juejin.im/post/5ae802a46fb9a07ac55fec04
- Java7编程高级进阶学习笔记
本书PDF 下载地址: http://pan.baidu.com/s/1c141KGS 密码:v6i1 注:本文有空会跟新: 讲述的是jdk7的内容: 注关于java 更详细的内容请进入:<Ja ...
- python之__dict__与dir(转载)
Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案. __dict__与dir()的区别: dir()是一个函数,返回的是list: __di ...
- myBatis各种依赖包
下载地址:myBatis各种依赖包
- CentOS 上安装 nodejs v11.0.0
下载 nodejs 淘宝 nodejs 镜像地址:https://npm.taobao.org/mirrors/node wget 命令下载: wget https://npm.taobao.org/ ...
- Code alignment 代码对齐改进(VS2017)
In mathematics you always keep your equals lined up directly underneath the one above. It keeps it c ...
- django项目验证码(PIL库实现)
作个记录,这个东东,是可以到处照搬的. WINDOWS下和LINUX下,字体对象获取方式不一样. 为了用户体验,字体大小也重要的. 另外,为了照顾个别字母认不出来的人,提供刷新功能. 一,验证码函数 ...
- Codeforces 924D Contact ATC (看题解)
Contact ATC 我跑去列方程, 然后就gg了... 我们计每个飞机最早到达时间为L[ i ], 最晚到达时间为R[ i ], 对于面对面飞行的一对飞机, 只要他们的时间有交集则必定满足条件. ...
- 024 SpringMvc的异常处理
一:说明 1.介绍 Springmvc提供HandlerExceptionResolver处理异常,包括Handler映射,数据绑定,以及目标方法执行. 2.几个接口的实现类 AnnotationMe ...
- spring security 获取当前用户信息
1.从页面上显示当前登陆的用户名 sec:authentication="name" <html xmlns:th="http://www.thymeleaf.or ...