zoukankan      html  css  js  c++  java
  • JSON 解析第三方框架

    常见的 JSON 解析第三方框架

    • JSONKit(最快)
    • SBJson
    • TouchJSON

    以上三个框架的性能依次降低!

    介绍 JSONKit 第三方框架的目的

    1. JSON 的解析并不是表面上那么简单
    2. 官方说 JSONKit 比苹果原生的 JSON 解析速度快!
    3. JSONKit 在很多老的项目中仍然在使用
    4. JSONKit 已经在 2012 年停止更新,适用于 iOS 5.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];
    

    性能测试

    static int largeNumber = 100 * 1000;
    
    - (void)jsonKitDemo {
        NSString *urlString = @"http://localhost/demo.json";
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    
            NSLog(@"start");
            CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
            for (int i = 0; i < largeNumber; ++i) {
                id result = [[JSONDecoder decoder] objectWithData:data];
            }
            NSLog(@"%f", CFAbsoluteTimeGetCurrent() - start);
    
            // 数据处理代码...
        }];
    }
    

    测试结果:

    • 时间:3.24s
    • 内存:4.74G

    苹果原生框架

    • 时间:0.18s
    • 内存:几乎不变
  • 相关阅读:
    react文本溢出hover气泡显示全部文本——JS判断文本溢出
    ecosystem.config
    git删除中间某次提交
    HTML5 新元素
    HTML5存储
    简述LVS(Linux虚拟服务器)
    简述线程池
    网络协议基础
    JVM常用性能检测工具
    线程间的“通信”方法
  • 原文地址:https://www.cnblogs.com/donghaoios/p/5089791.html
Copyright © 2011-2022 走看看