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 pandas使用chunksize异步拆分固定行数的文件
import pandas as pd import asyncio from collections import defaultdict collect = defaultdict(list) # ...
- CommandBehavior.CloseConnection有何作用
其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访 ...
- XAMPP中Apache和Mysql启动失败问题总结
一.Apache启动失败 xampp启动时显示的错误为: 9:52:41 [Apache] Attempting to start Apache app... 9:52:41 [Apache] ...
- Linux~win10上开启ubuntu子系统
在进行win10之后,我们可以在它上面安装一个linux子系统,然后就可以使用linux了,你不需要安装虚拟机,也不需要安装双系统! 1 通过Win10任务栏中的Cortana搜索框搜索打开“启用或关 ...
- ubuntu 修改 ls 下的目录颜色
ubuntu 下, ls 显示的目录的颜色,怎么说呢,看起来太费劲了. 于是想着修改成容易识别的颜色. 于是搜索了一下. 这里列举三个搜到的教程吧. 简单说我按这上面的方法做了,然后都失败了. 1. ...
- CentOS下输入输出重定向
nux重定向是指修改原来默认的一些东西,对原来系统命令的默认执行方式进行改变,比如说简单的我不想看到在显示器的输出而是希望输出到某一文件中就可以通过Linux重定向来进行这项工作. Linux默认输入 ...
- Timer 控件
1. 设置属性 Enable = true 或调用 start方法后, *_Tick 方法不会立即执行,会先等待一个时间间隔 2.timer1不管你上次的事情是否做完,它都会每个一个时间间隔做它应该 ...
- Silverlight & Blend动画设计系列十:Silverlight中的坐标系统(Coordinate System)与向量(Vector)运动
如果我们习惯于数学坐标系,那么对于Silverlight中的坐标系可能会有些不习惯.因为在Silverlight中的坐标系与Flash中的坐标系一样,一切都的颠倒的.在标准的数学坐标系中,X轴表示水平 ...
- C#中去除字符串里的多个空格且保留一个空格
static void Main(string[] args) { // 首先定义一个名为str 的字符串 string str="2 3 4 保留一个空格 ss ...
- 跨平台 GUI可视化 网络调试工具
mNetAssisthttp://blog.chinaunix.net/uid-21977056-id-4310527.htmlhttps://github.com/busyluo/mNetAssis ...