Question

with Data return from server it have a null value in it

How I can replace that null value with empty string by NSJSONSerialization or somthing else ?

my JSON data is :

{ 
 person:{
       [{name:name1,.....},{name:name2,....}]
      },
 class:{
       [{classname:null,.....},{classname:classname2,....}]
      }
}

Thanks for advance.

Was it helpful?

Solution 2

You need to iterate through the Dictionary for the keys has the Value of null:

Try similar:

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

Edit: I've not tested.

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

OTHER TIPS

Create Life Time Category to replace Null

Create Category (Replace null string with black space)

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

Create one more Category (Replace null array with black space)

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

Use Only Single Line and Enjoy with without Null Dictionary

#import "NSDictionary+NullReplacement.h"

NSDictionary * withoutNullResultDict=[resultDict dictionaryByReplacingNullsWithBlanks];

that same problem done with me and i can solve that by convert that data in NSMutableDicrionary and i continuously check data if any null data is come i replace with the blank string. try it may be it would help you.

-(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=@"";
    }
}

Used Below code :

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];
                }
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top