• 关于JSon中的NSNull


    后端返回的数据中总会出现一些NSNull类型,当我们一处理程序就会崩溃,因此想到把返回的数据中的NSNull类型全部转换成@""空字符串。下面是转化方法:

    1 自定义的几个方法:放在NSDictionary类别中

    #pragma mark - 私有方法

    //将NSDictionary中的Null类型的项目转化成@""

    +(NSDictionary *)nullDic:(NSDictionary *)myDic

    {

        NSArray *keyArr = [myDic allKeys];

        NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];

        for (int i = 0; i < keyArr.count; i ++)

        {

            id obj = [myDic objectForKey:keyArr[i]];

            

            obj = [self changeType:obj];

            

            [resDic setObject:obj forKey:keyArr[i]];

        }

        return resDic;

    }

    //将NSDictionary中的Null类型的项目转化成@""

    +(NSArray *)nullArr:(NSArray *)myArr

    {

        NSMutableArray *resArr = [[NSMutableArray alloc] init];

        for (int i = 0; i < myArr.count; i ++)

        {

            id obj = myArr[i];

            

            obj = [self changeType:obj];

            

            [resArr addObject:obj];

        }

        return resArr;

    }

    //将NSString类型的原路返回

    +(NSString *)stringToString:(NSString *)string

    {

        return string;

    }

    //将Null类型的项目转化成@""

    +(NSString *)nullToString

    {

        return @"";

    }

    #pragma mark - 公有方法

    //类型识别:将所有的NSNull类型转化成@""

    +(id)changeType:(id)myObj

    {

        if ([myObj isKindOfClass:[NSDictionary class]])

        {

            return [self nullDic:myObj];

        }

        else if([myObj isKindOfClass:[NSArray class]])

        {

            return [self nullArr:myObj];

        }

        else if([myObj isKindOfClass:[NSString class]])

        {

            return [self stringToString:myObj];

        }

        else if([myObj isKindOfClass:[NSNull class]])

        {

            return [self nullToString];

        }

        else

        {

            return myObj;

        }

    }

    2 使用方法(绿色的是实际用到的代码)

     

    (1)原始的json串:后端返回的json串里面包含类型NSString,NSArray,NSDictionary,NSNull类型。

     

    {"status":1,"service_name":null,"service_id":null,"img_url":"http://api.jgfw.me/assets/uploads/files/","price":null,"num":3,"service_info":{"service_type":null,"service_time":null,"service_detail":null,"customer_name":null,"customer_phone":null,"customer_address":"","new_jishi":"","old_jishi":null,"lat":null,"lon":null},"order_info":{"order_no":"E15031267469289848688","pay_time":null,"order_time":null,"price":0,"order_state":null}}

     

    (2)用SBJson库:json串转换成字典

     

    NSDictionary *jsonDic = [retString JSONValue];

    iOS解决json串中的NSNull类型

     

    (3)用自定义方法转换后的结果:将字典中所有的NSNull类型转换成@""

     

    NSDictionary *dict = [NSDictionary changeType:jsonDic];

     

    {

        "img_url" = "http://api.jgfw.me/assets/uploads/files/";

        num = 3;

        "order_info" =     {

            "order_no" = E15031267469289848688;

            "order_state" = "";

            "order_time" = "";

            "pay_time" = "";

            price = 0;

        };

        price = "";

        "service_id" = "";

        "service_info" =     {

            "customer_address" = "";

            "customer_name" = "";

            "customer_phone" = "";

            lat = "";

            lon = "";

            "new_jishi" = "";

            "old_jishi" = "";

            "service_detail" = "";

            "service_time" = "";

            "service_type" = "";

        };

        "service_name" = "";

        status = 1;

    }

    1
  • 相关阅读:
    [HNOI2002]营业额统计_Treap
    Catch That Cow_bfs
    Knight Moves
    Hie with the Pie
    tp5.1 错误 No input file specified.
    Jmeter通过BeanShell Sampler获取Jmeter的Bin路径,并存入变量供后面的脚本调用
    Jmeter如何把CSV文件的路径设置成一个变量,且变量的值是一个相对路径
    Autotest Weekly Report
    调试Javascript代码(浏览器F12)
    Javascript中escape()、encodeURI()、encodeURIComponent()的区别
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/5240027.html
Copyright © 2020-2023  润新知