Removing nulls from a JSON structure recursively

时光毁灭记忆、已成空白 提交于 2019-11-30 17:51:31

问题


I'm frequently finding the need to cache data structures created by NSJSONSerialization to disk and as -writeToFile fails if there are nulls, I need a fix that works when the structure is unknown. This works, and direct mutation is allowed as the instances of NSMutableDictionary themselves are not being enumerated, but it feels a bit hacky.

Is this totally fine or is it absolutely necessary to recreate a new tree and return that?

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

回答1:


There's nothing wrong with your general approach. Since NSNull is a singleton, it's fine to look for it by pointer comparison.

However, you're not recursing on the values in your dictionary. In general, those values might be arrays or dictionaries themselves. Perhaps in your specific case you know they're not. But if they could be, you need to perform removeNullsFromJSONTree: on each value in the dictionary.

You also don't look for NSNull in an array. Should you? It's trivial to handle:

[branch removeObject:[NSNull null]];

The removeObject: method removes all instances of the argument.

Personally I don't like testing object classes explicitly when I can use categories to let the message sending system do it for me. So instead I might define a category on NSObject like this:

// NSObject+KezRemoveNulls.h

@interface NSObject (KezRemoveNulls)

- (void)Kez_removeNulls;

@end

I would write a default do-nothing implementation for NSObject, and override it for NSMutableArray and NSMutableDictionary:

// 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

Note that all of the implementation code is still in one file.

Now I could say this:

id rootObject = [NSJSONSerialization JSONObjectWithData:...];
[rootObject Kez_removeNulls];



回答2:


Here's the code I'm using to clean up my JSON calls, seems to work well but, since there's some processing overhead involved I really only use it in situations where I can't do the null handling on the server. NSNull crashes is far and away our biggest app crash problem.

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

You call it by passing the NSData retrieved via NSURLConnection.

NSArray *uableData = [utility cleanJsonToObject:data];

or

NSDictionary *uableData = [utility cleanJsonToObject:data];



回答3:


Swift 4 version of @Travis M.'s answer;

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



回答4:


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


来源:https://stackoverflow.com/questions/16312879/removing-nulls-from-a-json-structure-recursively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!