什么是JSON?
JSON是一种轻量级的数据格式,一般用于数据交互。
服务器返回客户端的数据,一般都是JSON格式或者是XML格式(文件下载除外)。
资料网站:http://www.w3cschool.cc/
JSON 语法是 JavaScript对象表示法语法的子集。
- 数据在名称/值对中
- 数据由逗号分隔
- 花括号保存对象
- 方括号保存数组
JSON的格式很像OC中的字典和数组
{"name" : "jack", "age" : 10}
{"names" : ["jack", "rose", "jim"]}
标准JSON格式的注意点:key必须用双引号
要想从JSON中挖掘出具体数据,得对JSON进行解析
plist文件是苹果特有的。
在IOS中,JSON的常见解析方案有4种
第三方框架:JSONKit、SBJson、TouchJSON(性能从左到右依次变差)
苹果原生(自带):NSJSONSerialization(性能最好)
提示:JSON本质上是一个特殊格式的字符串,注意不是NSString,JSON的解析是一个非常繁琐的工作!
NSJSONSerialization的常见方法
JSON数据 —> OC对象
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
OC对象 —>JSON数据(客户端一般不会做这些事情,是服务器端做的)
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error
序列化与反序列化:
序列化:向服务器发送请求前,需要把提交给服务器的OC对象转换成二进制数据。
反序列化:从服务器接收到数据后,需要将服务器返回的二进制数据转换成OC对象。
使用苹果自带的JSON解析工程如下:
<span style="font-size:18px;">//
// ViewController.m
// JSON解析之反序列化
//
// Created by apple on 15/10/26.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1 url
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/videos.json"];
// 2. 请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 连接
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"result= %@", result);
NSLog(@"result=%@-----%@", result, [result class]); // 打印对象类型
}];
}
/** NSJSONSerialization方法中的枚举参数options —— 开发中不会使用
typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
NSJSONReadingMutableContainers = (1UL << 0),容器是可变的,转成后的结果是可变类型
NSJSONReadingMutableLeaves = (1UL << 1), 叶子节点是可变的
NSJSONReadingAllowFragments = (1UL << 2) 允许根节点可以不是字典或数组
} NS_ENUM_AVAILABLE(10_7, 5_0);
枚举类型,一般使用位移的,传0 什么事都不做
*/
@end</span>
JSONKit(知道就行)
================================================================================
之所以要知道的原因:
1> 官方说 JSONKit比苹果原生的JSON解析速度快!
2> JSONKit 在很多老的项目中仍然在使用
3> 有一个认识:JSON的解析并不是表面上那么简单
4> 这个框架在2012 年停止更新了,适用于 iOS5.0 以前的版本开发使用
5> 稍微了解一下 ARC & MRC混编的方法
使用步骤
1> 获得框架:https://github.com/johnezang/JSONKit
2> 添加文件
- JSONKit.h
- JSONKit.m
3> 设置 MRC标记
选择“项目”-“Build Phases”-“Compile Sources”
找到 JSONKit.m 并且在 Compiler Flags中添加 -fno-objc-arc
这样可以告诉编译器,这个在编译的时候,JSONKit.m不使用 ARC
4> 利用自动修复功能,修改两处 isa的错误
5> 编译
反序列化代码:
id result = [[JSONDecoder decoder] objectWithData:data];
NSLog(@"%@", result);
提示:工作中,如果碰到 JSON 解析,就用原生的就好了!
使用JSONKit解析如下,实现需要将JSONKit框架拉进工程,然后关闭json.m文件的ARC(即混编)方法如下:
关闭后仍然会有两个错误,点击错误的红色原点fix 按enter键即可。
代码如下:
<span style="font-size:18px;">//
// ViewController.m
// JSONKit解析数据
//
// Created by apple on 15/10/26.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
#import "JSONKit.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1 url
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/videos.json"];
// 2. 请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 连接
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 初始化JSON的一个解码器,调用方法解析
id result = [[JSONDecoder decoder] objectWithData:data];
NSLog(@"result= %@", result);
}];
}
@end</span>
它主要使用id result = [NSPropertyListSerializationpropertyListWithData:data options:0format:NULLerror:NULL];方法进行反序列化(即将服务器端的PList文件数据转化为对象类型)
代码如下:
<span style="font-size:18px;">//
// ViewController.m
// Plist文件的反序列化
//
// Created by apple on 15/10/26.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1 url
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/videos.plist"];
// 2. 请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 连接
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
// NSLog(@"result= %@", result);
id result = [NSPropertyListSerialization propertyListWithData:data options:0 format:NULL error:NULL];
NSLog(@"result=%@-----%@", result, [result class]); // 打印对象类型
}];
}
/**
typedef NS_OPTIONS(NSUInteger, NSPropertyListMutabilityOptions) {
NSPropertyListImmutable = kCFPropertyListImmutable, // 不可变
NSPropertyListMutableContainers = kCFPropertyListMutableContainers, // 整个数据容器可变
NSPropertyListMutableContainersAndLeaves = kCFPropertyListMutableContainersAndLeaves // 叶子节点和容器都可变
};
*/
@end</span>
运行结果没有截图,不再粘贴。
JSON数据解析与应用
本文详细介绍了JSON数据格式及其在数据交互中的应用,包括JSON语法、格式特点、解析方法及性能对比,还提供了使用苹果原生NSJSONSerialization、第三方框架如JSONKit、SBJson和TouchJSON进行JSON数据解析的实例,并探讨了如何处理PList文件的反序列化。
509

被折叠的 条评论
为什么被折叠?



