JSON解析详解(JSONKit、苹果自带解析)和PList文件的反序列化

JSON

什么是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

第三方框架:JSONKitSBJsonTouchJSON(性能从左到右依次变差)

苹果原生(自带):NSJSONSerialization(性能最好)

提示:JSON本质上是一个特殊格式的字符串,注意不是NSStringJSON的解析是一个非常繁琐的工作!


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>


PList文件的反序列化(虽然开发中不常用,因为PList是苹果特有的文件类型,而服务器端是面向安卓和苹果多种移动客户端的。)还是知道的好。

它主要使用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>

运行结果没有截图,不再粘贴。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值