我经常发现需要将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];
}
}
}
}
答案 0 :(得分:13)
你的一般方法没有错。由于NSNull
是单身,因此通过指针比较来查找它是可以的。
但是,您没有对字典中的值进行递归。通常,这些值可能是数组或字典本身。也许在你的具体情况下,你知道他们不是。但如果它们可以,则需要对字典中的每个值执行removeNullsFromJSONTree:
。
您也不会在数组中查找NSNull
。你应该?处理起来很简单:
[branch removeObject:[NSNull null]];
removeObject:
方法删除参数的所有实例。
我个人不喜欢明确地测试对象类,因为我可以使用类别让消息发送系统为我做。所以我可以在NSObject
上定义一个类别:
// NSObject+KezRemoveNulls.h
@interface NSObject (KezRemoveNulls)
- (void)Kez_removeNulls;
@end
我会为NSObject
编写默认的无操作实现,并覆盖NSMutableArray
和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
请注意,所有实现代码仍在一个文件中。
现在我可以这样说:
id rootObject = [NSJSONSerialization JSONObjectWithData:...];
[rootObject Kez_removeNulls];
答案 1 :(得分:10)
这是我用来清理我的JSON调用的代码,似乎运行良好但是,由于涉及一些处理开销,我实际上只在我无法在服务器上进行空值处理的情况下使用它。 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];
答案 2 :(得分: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
}
}
答案 3 :(得分:-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;
}