NSJSONSerialization能够处理的JSONData
NSJSONSerialization能够处理的JSONData

You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.
你用NSJSONSerialization这个类来将JSON数据转换成Foundation对象或者将Foundation对象转换成JSON数据。
An object that may be converted to JSON must have the following properties:
一个能被转换成JSON数据的对象必须具备以下的特性:
- The top level object is an NSArray or NSDictionary.
- All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
- All dictionary keys are instances of NSString.
- Numbers are not NaN or infinity.
- 最顶层的对象必须是NSArray或者NSDictionary
- 所有的实例对象必须是NSString、NSNumber、NSArray、NSDictionary或者NSNull
- 所有字典中的键值必须是NSString
- Numbers必须是有意义的(不能是无穷大)
提供测试使用的源码:
NSDictionary+JSON.h 与 NSDictionary+JSON.m
//
// NSDictionary+JSON.h
// Category
//
// Created by YouXianMing on 14-8-28.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h> @interface NSDictionary (JSON) // 转换成JSONString
- (NSString *)toJSONString; // 转换成JSONData
- (NSData *)toJSONData; @end
//
// NSDictionary+JSON.m
// Category
//
// Created by YouXianMing on 14-8-28.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "NSDictionary+JSON.h" @implementation NSDictionary (JSON) - (NSString *)toJSONString
{
NSData *data = [NSJSONSerialization dataWithJSONObject:self
options:NSJSONWritingPrettyPrinted
error:nil]; if (data == nil)
{
return nil;
} NSString *string = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
return string;
} - (NSData *)toJSONData
{
NSData *data = [NSJSONSerialization dataWithJSONObject:self
options:NSJSONWritingPrettyPrinted
error:nil]; return data;
} @end
NSData+JSON.h 与 NSData+JSON.m
//
// NSData+JSON.h
// Category
//
// Created by YouXianMing on 14-8-28.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h> @interface NSData (JSON) // 转换成集合
- (id)toPropertyList; @end
//
// NSData+JSON.m
// Category
//
// Created by YouXianMing on 14-8-28.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "NSData+JSON.h" @implementation NSData (JSON) - (id)toPropertyList
{
return [NSJSONSerialization JSONObjectWithData:self
options:NSJSONReadingMutableLeaves
error:nil];
} @end
使用时的源码:
//
// ViewController.m
// JSON
//
// Created by YouXianMing on 14-10-8.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 组织JSON数据
NSDictionary *jsonDic = \
@{
@"name" : @"YouXianMing", // NSString
@"age" : @, // NSNumber
@"BWH" : @[@, @, @], // NSArray
@"address" : @{@"BeiJin": @"XXXX", @"XianNing":@"YYYY"}, // NSDictionary
@"HasGirlFriend" : [NSNull null] // NSNull
}; // 转换成JSONData
NSData *jsonData = [jsonDic toJSONData]; // 将JSONData转换成list
NSLog(@"%@", [jsonData toPropertyList]);
} @end
以下是要点:


还有一点需要注意:
所有的数字相关的类型都会被转换成NSNumber,无论是布尔值还是浮点值还是整型值,都会被转换成NSNumber。
NSJSONSerialization能够处理的JSONData的更多相关文章
- iOS 自己封装的网络请求,json解析的类
基本上所有的APP都会涉及网络这块,不管是用AFNetWorking还是自己写的http请求,整个网络框架的搭建很重要. 楼主封装的网络请求类,包括自己写的http请求和AFNetWorking的请求 ...
- iOS 第三方框架-MJRefresh
MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单.github地址: https://github.com/CoderMJLee/MJRefresh . 下拉刷新 官方给过来的例子很简 ...
- iOS 调H5方法不执行没反应的坑
调用H5的方法需要给H5传一些参数,参数中包括图片的base64字符串. 错误一: 图片转base64,后面参数不能随便写,正确做法如下 NSData *imageData = UIImageJPEG ...
- NSJSONSerialization(json序列化)
//通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据 NSJSONSerialization isValidJSONObject:obj 我们能利用N ...
- NSJSONSerialization 组json字符串
抄的网上的. 主要是组织列表部分 NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:",@"lengt ...
- iOS下json的解析 NSJSONSerialization
- (IBAction)JOSNButtonPressed:(id)sender { NSString *str=[@"http://douban.fm/j/mine/playlist? ...
- NSJSONSerialization介绍
ios5中apple增加了解析JSON的api——NSJSONSerialization.网上已经有人做过测试,NSJSONSerialization在效率上完胜SBJSON.TouchJSON. ...
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...
- Swift - 解析JSON数据(内置NSJSONSerialization与第三方JSONKit)
一,使用自带的NSJSONSerialization 苹果从IOS5.0后推出了SDK自带的JSON解决方案NSJSONSerialization,这是一个非常好用的JSON生成和解析工具,效率也比其 ...
随机推荐
- 高版本sonar安装遇到的坑-sonar 6.7.5
最近安装了6.7.5版本的sonar,发现里面的坑还是很多,下面列举下遇到的坑 sonar插件地址:https://docs.sonarqube.org/display/PLUG/Plugin+Lib ...
- SQL操作Json数据
转载自: http://blog.csdn.net/yapingxin/article/details/16913275 有小改动.. 支持复杂结构的使用.. 使用Parent_ID来对应Object ...
- 【DB2】普通用户最小查询权限分配
1. 通过实例用户或者有dbadm权限的用户连接数据库 db2 connect to <db-name> 2. 分配普通用户连接权限db2 "grant connect on d ...
- springboot整合spring data jpa 动态查询
Spring Data JPA虽然大大的简化了持久层的开发,但是在实际开发中,很多地方都需要高级动态查询,在实现动态查询时我们需要用到Criteria API,主要是以下三个: 1.Criteria ...
- Js正则Replace方法
JS正则的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\s+)|(\s+$)" ...
- Android创建定时和周期任务
问题:应用需要按时执行某个操作,例如定时更新UI. 解决方案:使用Handler提供的定时操作功能.通过Handler,可以在指定的时间或是指定的延时后执行操作. 下面看一个在TextView中显示当 ...
- .net EF框架-实现增删改查
声明一个EF上下文对象 Model dbContext = new Model(); 添加操作(向表中插入一条数据) //声明一个表的实体 Contact contact = new Contact( ...
- 2017年12月14日 LinQ高级查&&Asp.net WebForm Asp.net MVC
LinQ的高级查询用法 开头:StartsWith()结尾:EndsWith()模糊:Contains() 个数:Count最大值:Max(r => r.price)最小值:Min(r => ...
- Ubuntu 16.04 开启BBR加速
BBR(Bottleneck Bandwidth and RTT)是Google推出的一个提高网络利用率的算法,可以对网络进行加速,用来干什么大家心里都有B数 Ubuntu开启BBR的前提是内核版本必 ...
- Python中@修饰符的作用。
'@'符号用作函数修饰符是python2.4新增加的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.也就是说@A def f(): 是非法的. 只可以在模块或类定义层内对函数进行修饰, ...