Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone应用程序JSON响应的非空验证_Iphone_Objective C_Json_Web Services - Fatal编程技术网

iPhone应用程序JSON响应的非空验证

iPhone应用程序JSON响应的非空验证,iphone,objective-c,json,web-services,Iphone,Objective C,Json,Web Services,目前,我正在使用以下方法验证数据是否为空 if ([[response objectForKey:@"field"] class] != [NSNull class]) NSString *temp = [response objectForKey:@"field"]; else NSString *temp = @""; 当响应字典包含数百个属性(以及相应的值)时,问题就会出现。我需要将这种条件添加到字典的每个元素中 还有其他方法可以实现吗 有没有对web服务进行任何更改的建

目前,我正在使用以下方法验证数据是否为空

if ([[response objectForKey:@"field"] class] != [NSNull class])
    NSString *temp = [response objectForKey:@"field"];
else
    NSString *temp = @"";
当响应字典包含数百个属性(以及相应的值)时,问题就会出现。我需要将这种条件添加到字典的每个元素中

还有其他方法可以实现吗

有没有对web服务进行任何更改的建议(不向数据库插入空值除外)


任何人,有什么想法吗?

首先有一个小点:你的测试不是惯用的,你应该使用

if (![[response objectForKey:@"field"] isEqual: [NSNull null]])
如果要将字典中所有值为
[NSNull null]
的键重置为空字符串,最简单的修复方法是

for (id key in [response allKeysForObject: [NSNull null]])
{
    [response setObject: @"" forKey: key];
}
以上假设
响应
是一个可变字典


然而,我认为你真的需要重新审视你的设计。如果数据库中不允许使用
[NSNull null]
值,则不应允许这些值。

我不太清楚您需要什么,但:

如果需要检查key的值是否为NULL,可以执行以下操作:

for(NSString* key in dict) {
   if( ![dict valueForKey: key] ) {
       [dict setValue: @"" forKey: key];
   }
}
static NSArray* req_keys = [[NSArray alloc] initWithObjects: @"k1", @"k2", @"k3", @"k4", nil];
如果您有一些必需的密钥集,则可以创建静态数组,然后执行以下操作:

for(NSString* key in dict) {
   if( ![dict valueForKey: key] ) {
       [dict setValue: @"" forKey: key];
   }
}
static NSArray* req_keys = [[NSArray alloc] initWithObjects: @"k1", @"k2", @"k3", @"k4", nil];
然后在检查数据的方法中:

NSMutableSet* s = [NSMutableSet setWithArray: req_keys];

NSSet* s2 = [NSSet setWithArray: [d allKeys]];

[s minusSet: s2];
if( s.count ) {
    NSString* err_str = @"Error. These fields are empty: ";
    for(NSString* field in s) {
        err_str = [err_str stringByAppendingFormat: @"%@ ", field];
    }
    NSLog(@"%@", err_str);
}

我所做的是在字典上加上一个类别

@interface NSDictionary (CategoryName)

/**
 * Returns the object for the given key, if it is in the dictionary, else nil.
 * This is useful when using SBJSON, as that will return [NSNull null] if the value was 'null' in the parsed JSON.
 * @param The key to use
 * @return The object or, if the object was not set in the dictionary or was NSNull, nil
 */
- (id)objectOrNilForKey:(id)aKey;



@end


@implementation NSDictionary (CategoryName)

- (id)objectOrNilForKey:(id)aKey {
    id object = [self objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
那你就可以用

[response objectOrNilForKey:@“field”]


如果愿意,您可以修改它以返回一个空白字符串。

很好的技巧。完美的解决方案。谢谢大量使用类别。
static inline NSDictionary* DictionaryRemovingNulls(NSDictionary *aDictionary) {

  NSMutableDictionary *returnValue = [[NSMutableDictionary alloc] initWithDictionary:aDictionary];
  for (id key in [aDictionary allKeysForObject: [NSNull null]]) {
    [returnValue setObject: @"" forKey: key];
  }
  return returnValue;
}


response = DictionaryRemovingNulls(response);