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生成和解析工具,效率也比其 ...
随机推荐
- (转)python之os,sys模块详解
python之sys模块详解 原文:http://www.cnblogs.com/cherishry/p/5725184.html sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和 ...
- JetBrains PyCharm(Professional版本)的下载、安装和初步使用
不多说,直接上干货! 首先谈及这款软件,博主我用的理由:搞机器学习和深度学习! 想学习Python的同学们,在这里隆重介绍一款 Python 的开发工具 pyCharm IDE.这是我最喜欢的 Pyt ...
- SQL Cookbook—数字、日期
1.计算不包含最大值和最小值的均值2.把字母数字串转换为数值3.更改累计和中的值–显示存款或取款后的值4.加减日.月.年5.计算两个日期之间的天数6.确定两个日期之间的工作日数目表EMP中,计算BLA ...
- Redis双机热备方案--转
http://luyx30.blog.51cto.com/1029851/1350832 参考资料: http://patrick-tang.blogspot.com/2012/06/redis-ke ...
- whatwg-fetch
fetch 是什么 XMLHttpRequest的最新替代技术 fetch优点 接口更简单.简洁,更加语义化 基于promise,更加好的流程化控制,可以不断then把参数传递,外加 async/aw ...
- 基于resteasy,Base64码上传文件
package com.xgt.controller.bs; import com.xgt.bean.bs.VersionBean; import com.xgt.common.BaseControl ...
- 计算两个NSDate之间,相隔多少秒数
计算两个NSDate之间,相隔多少秒数 //两个时间间隔秒数 - (NSInteger)intervalSecondsWithSmallDate:(NSDate*)smallDate bigDate: ...
- PostgreSQL Entity Framework 自动迁移
1.依次添加NuGet包 EntityFramework.Npgsql.EntityFramework6.Npgsql,会自动生成一些配置文件,不过缺少数据库驱动的配置节点: <system.d ...
- 1.http请求编程-->基础原理
一.技术分析 打开网页,不管我们请求的是静态资源还是动态资源,IIS都会根据ISAPI(微软和Process软件公司联合提出的Web服务器上的API标准)这一标准,将请求的文件根据文件后缀名的不同,转 ...
- Spring 数据传入
表单传入 前端代码: <form method="POST" id="user_login_submit"> <div class=" ...