从服务器返回数据,其中包含空值
如何用NSJSONSerialization或其他东西用空字符串替换空值?
我的JSON数据是:
{
person:{
[{name:name1,.....},{name:name2,....}]
},
class:{
[{classname:null,.....},{classname:classname2,....}]
}
}
感谢您提前。
答案 0 :(得分:2)
创建生命时间类别以替换Null
创建类别(用空格替换空字符串)
NSDictionary + NullReplacement.h
#import <Foundation/Foundation.h>
@interface NSDictionary (NullReplacement)
- (NSDictionary *)dictionaryByReplacingNullsWithBlanks;
@end
的NSDictionary + NullReplacement.m
#import "NSDictionary+NullReplacement.h"
#import "NSArray+NullReplacement.h"
@implementation NSDictionary (NullReplacement)
- (NSDictionary *)dictionaryByReplacingNullsWithBlanks {
const NSMutableDictionary *replaced = [self mutableCopy];
const id nul = [NSNull null];
const NSString *blank = @"";
for (NSString *key in self) {
id object = [self objectForKey:key];
if (object == nul) [replaced setObject:blank forKey:key];
else if ([object isKindOfClass:[NSDictionary class]]) [replaced setObject:[object dictionaryByReplacingNullsWithBlanks] forKey:key];
else if ([object isKindOfClass:[NSArray class]]) [replaced setObject:[object arrayByReplacingNullsWithBlanks] forKey:key];
}
return [NSDictionary dictionaryWithDictionary:[replaced copy]];
}
@end
再创建一个类别(用黑色空格替换空数组)
的NSArray + NullReplacement.h
#import <Foundation/Foundation.h>
@interface NSArray (NullReplacement)
- (NSArray *)arrayByReplacingNullsWithBlanks;
@end
的NSArray + NullReplacement.m
#import "NSArray+NullReplacement.h"
#import "NSDictionary+NullReplacement.h"
@implementation NSArray (NullReplacement)
- (NSArray *)arrayByReplacingNullsWithBlanks {
NSMutableArray *replaced = [self mutableCopy];
const id nul = [NSNull null];
const NSString *blank = @"";
for (int idx = 0; idx < [replaced count]; idx++) {
id object = [replaced objectAtIndex:idx];
if (object == nul) [replaced replaceObjectAtIndex:idx withObject:blank];
else if ([object isKindOfClass:[NSDictionary class]]) [replaced replaceObjectAtIndex:idx withObject:[object dictionaryByReplacingNullsWithBlanks]];
else if ([object isKindOfClass:[NSArray class]]) [replaced replaceObjectAtIndex:idx withObject:[object arrayByReplacingNullsWithBlanks]];
}
return [replaced copy];
}
@end
仅使用单行并享受无空字典
#import "NSDictionary+NullReplacement.h"
NSDictionary * withoutNullResultDict=[resultDict dictionaryByReplacingNullsWithBlanks];
答案 1 :(得分:1)
您需要iterate
通过Dictionary
键,其值为 null :
尝试类似:
NSArray *keys= [json allKeys];
for (NSString *keysV in keys){
NSLog(@"Keys are %@", keysV);
if([[Your_Dict objectForKey: keysV] isEqual:[NSNull null]]){
//Do your stuff here
}
}
编辑:我没有测试过。
NSArray *keys= [json allKeys];
for (NSString *keysV in keys){
for(NSDictionary *subDict in [yourDict objectForKey: keysV]){
NSArray *subKeys = [subDict allKeys];
for (NSString *keysVv in subKeys){
if([[subDict objectForKey: keysVv] isEqual:[NSNull null]]){
//Do your stuff here
}
}
}
}
答案 2 :(得分:0)
同样的问题与我完成,我可以通过转换NSMutableDicrionary
中的数据来解决这个问题,如果有任何空数据,我会不断检查数据,我用空字符串替换。
试试它可能对你有帮助。
-(void)ConfigureCell:(int)Section :(int)Index :(NSMutableArray *)threadArray{
NSMutableDictionary *threadDict=[threadArray objectAtIndex:Index];
if( [[threadDict objectForKey:@"read"]boolValue]){
}
CGRect subjectLabelSize;
self.contactNameLabel.text=@"1323123123123";
self.lastMessageLabel.text=[threadDict objectForKey:@"lastMessage"];
if ([[threadDict objectForKey:@"subject"] isEqual:[NSNull null]]) {
self.subjectLabel.text=@"";
}
}
答案 3 :(得分:0)
使用下面的代码:
for (NSDictionary * test in Your_Array)
{
for (id dict in [test allKeys] )
{
([test valueForKey:dict] == [NSNull null]) ? [test setValue:@"" forKey:dict] :[test setValue:[test valueForKey:dict] forKey:dict];
}
}