5

我经常发现需要将创建的数据结构缓存NSJSONSerialization到磁盘,-writeToFile如果有空值会失败,我需要一个在结构未知时有效的修复程序。这是可行的,并且允许直接变异,因为 NSMutableDictionary 本身的实例没有被枚举,但感觉有点 hacky。

这完全没问题还是绝对有必要重新创建一棵新树并返回它?

- (void) removeNullsFromJSONTree:(id) branch
{
    if ([branch isKindOfClass:[NSMutableArray class]])
    {
        //Keep drilling to find the leaf dictionaries
        for (id childBranch in branch)
        {
            [self removeNullsFromJSONTree:childBranch];
        }
    }
    else if ([branch isKindOfClass:[NSMutableDictionary class]])
    {
        const id nul = [NSNull null];
        const NSString *empty = @"";
        for(NSString *key in [branch allKeys])
        {
            const id object = [branch objectForKey:key];
            if(object == nul)
            {
                [branch setObject:empty forKey:key];
            }
        }
    }
}
4

4 回答 4

14

你的一般方法没有错。由于NSNull是单例,通过指针比较来查找就可以了。

但是,您不会递归字典中的值。通常,这些值可能是数组或字典本身。也许在您的具体情况下,您知道他们不是。但如果可能的话,您需要对removeNullsFromJSONTree:字典中的每个值执行。

您也不必NSNull在数组中查找。你应该?处理起来很简单:

[branch removeObject:[NSNull null]];

removeObject:方法删除参数的所有实例。

当我可以使用类别让消息发送系统为我做这件事时,我个人不喜欢明确地测试对象类。因此,我可能会NSObject像这样定义一个类别:

// NSObject+KezRemoveNulls.h

@interface NSObject (KezRemoveNulls)

- (void)Kez_removeNulls;

@end

我会为 编写一个默认的无操作实现,并为andNSObject覆盖它:NSMutableArrayNSMutableDictionary

// NSObject+KezRemoveNulls.m

#import "NSObject+KezRemoveNulls.h"

@implementation NSObject (KezRemoveNulls)

- (void)Kez_removeNulls {
    // nothing to do
}

@end

@implementation NSMutableArray (KezRemoveNulls)

- (void)Kez_removeNulls {
    [self removeObject:[NSNull null]];
    for (NSObject *child in self) {
        [child Kez_removeNulls];
    }
}

@end

@implementation NSMutableDictionary (KezRemoveNulls)

- (void)Kez_removeNulls {
    NSNull *null = [NSNull null];
    for (NSObject *key in self.allKeys) {
        NSObject *value = self[key];
        if (value == null) {
            [self removeObjectForKey:key];
        } else {
            [value Kez_removeNulls];
        }
    }
}

@end

请注意,所有实现代码仍在一个文件中。

现在我可以这样说:

id rootObject = [NSJSONSerialization JSONObjectWithData:...];
[rootObject Kez_removeNulls];
于 2013-05-01T05:22:27.427 回答
10

这是我用来清理我的 JSON 调用的代码,似乎运行良好,但是由于涉及一些处理开销,我真的只在我无法在服务器上进行 null 处理的情况下使用它。NSNull 崩溃是我们最大的应用程序崩溃问题。

+ (id)cleanJsonToObject:(id)data {
    NSError* error;
    if (data == (id)[NSNull null]){
        return [[NSObject alloc] init];
    }
    id jsonObject;
    if ([data isKindOfClass:[NSData class]]){
        jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    } else {
        jsonObject = data;
    }
    if ([jsonObject isKindOfClass:[NSArray class]]) {
        NSMutableArray *array = [jsonObject mutableCopy];
        for (int i = array.count-1; i >= 0; i--) {
            id a = array[i];
            if (a == (id)[NSNull null]){
                [array removeObjectAtIndex:i];
            } else {
                array[i] = [self cleanJsonToObject:a];
            }
        }
        return array;
    } else if ([jsonObject isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *dictionary = [jsonObject mutableCopy];
        for(NSString *key in [dictionary allKeys]) {
            id d = dictionary[key];
            if (d == (id)[NSNull null]){
                dictionary[key] = @"";
            } else {
                dictionary[key] = [self cleanJsonToObject:d];
            }
        }
        return dictionary;
    } else {
        return jsonObject;
    }
}

您通过传递通过 NSURLConnection 检索到的 NSData 来调用它。

NSArray *uableData = [utility cleanJsonToObject:data];

或者

NSDictionary *uableData = [utility cleanJsonToObject:data];
于 2013-08-30T19:46:47.770 回答
0

@Travis M. 答案的 Swift 4 版本;

class func removeNullFromJSONData(_ JSONData: Any) -> Any {
    if JSONData as? NSNull != nil {
        return JSONData
    }

    var JSONObject: Any!

    if JSONData as? NSData != nil {
        JSONObject = try! JSONSerialization.data(withJSONObject: JSONData, options: JSONSerialization.WritingOptions.prettyPrinted)
    }
    else {
        JSONObject = JSONData
    }

    if JSONObject as? NSArray != nil {
        let mutableArray: NSMutableArray = NSMutableArray(array: JSONObject as! [Any], copyItems: true)
        let indexesToRemove: NSMutableIndexSet = NSMutableIndexSet()

        for index in 0 ..< mutableArray.count {
            let indexObject: Any = mutableArray[index]

            if indexObject as? NSNull != nil {
                indexesToRemove.add(index)
            }
            else {
                mutableArray.replaceObject(at: index, with: removeNullFromJSONData(indexObject))
            }
        }

        mutableArray.removeObjects(at: indexesToRemove as IndexSet)

        return mutableArray;
    }
    else if JSONObject as? NSDictionary != nil {
        let mutableDictionary: NSMutableDictionary = NSMutableDictionary(dictionary: JSONObject as! [AnyHashable : Any], copyItems: true)

        for key in mutableDictionary.allKeys {
            let indexObject: Any = mutableDictionary[key] as Any

            if indexObject as? NSNull != nil {
                mutableDictionary.removeObject(forKey: key)
            }
            else {
                mutableDictionary.setObject(removeNullFromJSONData(indexObject), forKey: key as! NSCopying)
            }
        }

        return mutableDictionary
    }
    else {
        return JSONObject
    }
}
于 2018-03-30T15:53:15.850 回答
-1
+ (id)getObjectWithoutNullsForObject:(id)object
{
    id objectWithoutNulls;

    if ([object isKindOfClass:[NSDictionary class]])
    {
        NSMutableDictionary *dictionary = ((NSDictionary *)object).mutableCopy;

        [dictionary removeObjectsForKeys:[dictionary allKeysForObject:[NSNull null]]];

        for (NSString *key in dictionary.allKeys)
        {
            dictionary[key] = [self getObjectWithoutNullsForObject:dictionary[key]];
        }

        objectWithoutNulls = dictionary;
    }
    else if ([object isKindOfClass:[NSArray class]])
    {
        NSMutableArray *array = ((NSArray *)object).mutableCopy;

        [array removeObject:[NSNull null]];

        for (NSUInteger index = 0; index < array.count; index++)
        {
            array[index] = [self getObjectWithoutNullsForObject:array[index]];
        }

        objectWithoutNulls = array;
    }
    else if ([object isKindOfClass:[NSNull class]])
    {
        objectWithoutNulls = Nil;
    }
    else
    {
        objectWithoutNulls = object;
    }

    return objectWithoutNulls;
}
于 2016-10-31T13:23:15.253 回答