iOS開發解決json串中的NSNull類型

來源:互聯網
上載者:User

標籤:

後端返回的資料中總會出現一些NSNull類型,當我們一處理常式就會崩潰,因此想到把返回的資料中的NSNull類型全部轉換成@""Null 字元串。下面是轉化方法:

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];

 (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;

 

}

iOS開發解決json串中的NSNull類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.