zoukankan      html  css  js  c++  java
  • 关于网络开发中JSON的使用

            JSON是一种轻量级的数据交换格式,非常适合网络间的数据交互。JSON的格式和OC中的字典和数组类似。

    key-value格式的:

    {“name”:“Tom”, "age":20}

    数组格式的:

    ["Tom", "Jack"]

            在iOS开发中如果要使用JSON,就需要对JSON进行解析。在iOS中,JSON的解析方案主要有以下四中解析方案:

            1、NSJSONSerialization

            2、JSONKit 

            3、SBJson

            4、TouchJSON

    上面四种方案从上到下性能越来越差,其中第一个方案是苹果自带解决方案,性能最优,推荐这种方案。后面三种属于第三方框架,可以在Github上面进行了解。下面着重说一下NSJSONSerialization。

            

            1、JSON转OC对象

             JSON转OC对象有两个类方法

    + (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
    
    + (id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error;

    第一个类方法是通过NSData的类型的JSON数据创建OC对象,第二种是通过NSInputStream类型的JSON数据流创建OC对象,option参数的作用源码有如下注释:

    Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects.

    NSJSONReadingMutableContainers: 返回可变容器,如NSMutableArray、NSMutableDictionary

    NSJSONReadingMutableLeaves: 返回NSMutableString对象

    NSJSONReadingAllowFragments: 允许JSON字符串最外层既不是数组也不是字典,但必须是有效的JSON Fragment,例如@"123"。下面看测试代码:

    - (void)sendDemo0 {
        //验证用户名是否为空
        NSString *userName = self.userNameField.text;
        if (userName.length<=0) {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [hud hide:YES afterDelay:1];
            [hud setLabelText:@"用户名不能为空"];
            [hud setMode:MBProgressHUDModeCustomView];
            return;
        }
        //验证密码是否为空
        NSString *pwd = self.userPwdField.text;
        if (pwd.length<=0) {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].windows.lastObject animated:YES];
            [hud hide:YES afterDelay:1];
            [hud setLabelText:@"密码不能为空"];
            [hud setMode:MBProgressHUDModeCustomView];
            return;
        }
        //显示一个加载框
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].windows.lastObject animated:YES];
        hud.labelText = @"正在加载";
        hud.dimBackground = YES;
        hud.removeFromSuperViewOnHide = YES;
        
        //发送HTTP请求,进行登录
        NSString *str = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@", userName, pwd];
        NSURL *url = [NSURL URLWithString:str];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            //对返回的数据进行处理,例如json解析
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            //显示提示信息
            MBProgressHUD *messageHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            messageHud.removeFromSuperViewOnHide = YES;
            messageHud.mode = MBProgressHUDModeText;
            [messageHud show:YES];
            NSString *error = [dict objectForKey:@"error"];
            if (error) {
                messageHud.labelText = error;
            }else {
                messageHud.labelText = [dict objectForKey:@"success"];
            }
            [messageHud hide:YES afterDelay:1];
            
            [hud hide:YES];
        }];
        
    }

    MBProgressHUD是第三方框架,用来显示一个加载框或者消息提示框,NSURLConnection是用来发送网络请求获取json数据的,这两个类现在这用,以后专门说一下。

       2、OC对象转JSON

        OC对象转JSON有一个静态方法:

    + (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;

    参数obj应该是一个有效的JSON格式,如果提供了一个无效的JSON,这个方法将会抛出异常,检测是否有效用这个方法:

    + (BOOL)isValidJSONObject:(id)obj;

    如果有效则返回YES,否则返回NO,有效的对象必须有以下属性:

        - Top level object is an NSArray or NSDictionary
        - All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull
        - All dictionary keys are NSStrings
        - NSNumbers are not NaN or infinity

    最外层的对象时NSArray或者NSDictionary,里面的对象是NSString、NSNumber、NSArray、NSDictionary或者NSNull,所有字典的key必须是NSString类型,NSNumber不能是NaN或者无穷大。

    opt参数只有一个选择:NSJSONWritingPrettyPrinted,

    Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable.

    设置这个参数后可以格式化JSON数据,使输出结果更可读。测试代码如下:(接着上面的测试代码)

    NSData *data1 = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
            NSString *str = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
            NSLog(@"str:%@", str);

    输出结果如下:

    2015-06-30 10:13:48.308 HttpDemo[56455:607] str:{
      "error" : "用户名不存在"
    }


    最后附上测试demo:Demo

  • 相关阅读:
    UIPickerView-一.01-
    闭包-01-Swift
    Swift 入门-01-概述
    git使用命令行-01-自己操作的
    FetchedResultsController-03-CoreData相关
    SQLite-05-增删改查
    Sqlite函数总结-04
    List<string>转xml
    比较两个List<T>是否相同
    获取DataTable前几条数据
  • 原文地址:https://www.cnblogs.com/arthas/p/4659957.html
Copyright © 2011-2022 走看看