1. 程式人生 > >iOS去除網路請求到的資料的null值

iOS去除網路請求到的資料的null值

請求介面獲取到的資料包涵null值,處理的時候遇到了問題。

在網上搜到了幾種解決辦法,自己嘗試了兩種都可以達到目的,在這裡記錄一下。

第一種方法是使用分類給字典新增一個類方法,將字典中的null值全部替換為空字串,程式碼如下:

.h檔案程式碼:

#import <Foundation/Foundation.h>

@interface NSDictionary (DeleteNull)

+(id)changeType:(id)myObj;

@end

.m檔案程式碼:

#import "NSDictionary+DeleteNull.h"

@implementation NSDictionary (DeleteNull)

#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;
    }
}

@end
使用方法:
[model setValuesForKeysWithDictionary:[NSDictionary changeType:dict]];


第二種方法是利用AFNetworking的自動解析,去除掉值為null的鍵值對,程式碼如下:
_manager = [AFHTTPRequestOperationManager manager];
    AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
    response.removesKeysWithNullValues = YES;
    _manager.responseSerializer = response;
    _manager.requestSerializer = [AFJSONRequestSerializer serializer];


相關推薦

iOS去除網路請求到的資料null

請求介面獲取到的資料包涵null值,處理的時候遇到了問題。 在網上搜到了幾種解決辦法,自己嘗試了兩種都可以達到目的,在這裡記錄一下。 第一種方法是使用分類給字典新增一個類方法,將字典中的null值全部替換為空字串,程式碼如下: .h檔案程式碼: #import <Fo

iOS網路請求資料轉換成plist檔案

需求設想: iOS儲存方式有很多,我就不一一說明了,現在我只提到我用到的。 現在有個需求是將部分資料存到本地,並且在沒有網路的情況下能夠檢視資料。 根據這個需求,也就是需要做一個數據本地化儲存。 那麼我們可以想到有呢麼幾種方式: 1.NSUserDefaults 

iOS Http網路請求、快取、及網路資料更新才請求

iOS網路快取掃盲篇 --使用兩行程式碼就能完成80%的快取需求 目錄 由於微信、QQ、微博、這類的應用使用快取很“重”,使一般的使用者也對快取也非常習慣。快取已然成為必備。 快取的目的的以空間換時間 這句話在動輒就是 300M、600M 的大應用上,得到了

網路請求資料null 的處理

AFN框架       ((AFJSONResponseSerializer *)_shareClient.responseSerializer).removesKeysWithNullValues 

iOS HTTP網路請求Cookie的讀取與寫入(NSHTTPCookieStorage)

當你訪問一個網站時,NSURLRequest都會幫你主動記錄下來你訪問的站點設定的Cookie,如果 Cookie 存在的話,會把這些資訊放在 NSHTTPCookieStorage 容器中共享,當你下次再訪問這個站點時,NSURLRequest會拿著上次儲存下來了的Cookie繼續去請求。 同樣適

側滑+fragment切換頁面+fragment巢狀+二次取樣+輪播圖+gridview展示圖片+網路請求資料+資料庫

全域性配置Appliction 所需要的依賴有:implementation ‘com.google.code.gson:gson:2.8.5’ implementation ‘com.nostra13.universalimageloader:universal-image-loader:

Http網路請求資料解析json展示資料+網路圖片展示+資料庫+Viewpager+Fragment

1.MainActivity主頁面 package com.bwie.guoxinyu; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import and

OKHTTP-RecyclerView實現網路請求資料

build.gradle中匯入需要依賴 implementation ‘com.google.code.gson:gson:2.8.5’ implementation ‘com.android.support:design:27.1.1’ compile ‘cn.yipianfengye

Fragment標籤頁+OKHttp網路請求資料+MVP模式

分包方式 需要的第三方依賴 Fragment 新建兩個fragment MainActivity 主頁面佈局 設定Fragment+tablayout的介面卡 Fragment標籤頁結束 OKHttp網路請求 okhttputils工具類

網路請求資料(Uitl工具類)

封裝工具類 package com.example.pullshuaixn; import android.annotation.SuppressLint; import android.os.AsyncTask; import com.google.gson.Gson;

JS綜合開發 頂部+導航+網路請求+資料顯示+跳轉!!!

首先設定導航: import React, { Component } from "react"; import { createStackNavigator } from "react-navigation"; import TopNav from "./TopNav";

android快取框架ASimpleCache的使用 (網路請求資料並快取)

官方簡介: ASimpleCache 是一個為Android制定的 輕量級的 開源快取框架。輕量到只有一個java檔案(由十幾個類精簡而來)。 框架地址 1、它可以快取什麼東西? 普通的字串、JsonObject、JsonArray、Bitmap、Drawable、

網路請求資料的viewpager

import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCo

Android開發-網路請求資料

如何用網路請求資料呢? 第一步 建立一個工具類NetWorks 在工具類中寫一個方法(getJson) 這個類裡可以寫很多方法 不僅僅限於網路請求資料 比如常見的還有判斷網路的連線狀態等等…在其他的頁面可以呼叫到這個工具類裡的方法 public class NetWorks {

Android Retrofit網路請求資料顯示

json解析資料 網路請求許可權 <!--加上網路許可權--> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission a

九宮格網路請求資料

寫在fragment裡面 R.layout.home_fragment.xml佈局 <GridView android:id="@+id/grid_view" android:layout_width="match_parent" android

網路請求資料解析---Swift

資料解析我們需要拿到介面部分的名稱 根據工程中的MVC設計模式進行傳值 獲取 我們需要用到第三方的資料解析方法 點選獲取第三方 匯入自己工程中 匯入標頭檔案 建立model類 M 例如: class model: NSObject { // coverLar

OkHttp網路請求資料

OkHttpUtils package com.bawei.mymvp.okHttp; import android.os.Handler; import android.os.Looper; import com.bawei.mymvp.Constants; import com.

RecyclerView多條目載入(網路請求資料

RecyclerView多條目載入和ListView的多條目載入用法是一樣的,程式碼如下: 1.匯入RecyclerView或XRecyclerView的依賴包(自己按照需求匯入依賴包,下面RecyclerView可以替換成XRecyclerView)XRecyclerVi

[Swift通天遁地]四、網路和執行緒-(5)解析網路請求資料:String(字串)、Data(二進位制資料)和JSON資料

本文將演示如何解析網路請求資料:使用Alamofire的Get請求並輸出字串(String)、二進位制資料(Data)和JSON資料。 首先確保在專案中已經安裝了所需的第三方庫。 點選【Podfile】,檢視安裝配置檔案。 1 source 'https://github.com/CocoaPod