I have a JSON Feed:
{
"count1" = 2;
"count2" = 2;
idval = 40;
level = "<null>";
"logo_url" = "/assets/logos/default_logo_medium.png";
name = "Golf Club";
"role_in_club" = Admin;
}
The problem is the "<null>"
that I cannot figure out how to remove from the NSDictionary before saving it to NSUserDefaults.
Please help me solve this issue. Thankyou!
Swift 3.0/4.0
SolutionFollowing is the solution, when
JSON
havesub-dictionaries
. This will go-through all thedictionaries
, sub-dictionaries
ofJSON
and removeNULL(NSNull) key-value
pair from theJSON
.Here is my category-based recursive solution for dictionaries containing dictionaries and arrays which values can also be dictionaries and arrays:
File NSDictionary+Dario.m:
File NSArray+Dario.m:
Use this for remove null from dictionary
the following code is ok when the result is in array or dictionary , you can change the result returned to nil or empty string by editing the code
the function is recursive , so can parse array in dictionary .
jsonresult ( { Description = ""; Id = 1; Name = High; }, { Description = ""; Id = 2; Name = Medium; }, { Description = ""; Id = 3; Name = Low; } )
change null ( { Description = ""; Id = 1; Name = High; }, { Description = ""; Id = 2; Name = Medium; }, { Description = ""; Id = 3; Name = Low; } )
To remove it, convert it into a mutable dictionary and remove the object for key "level";
If you don't use ARC, you'll need to add some calls to 'release'.
Update:
If you don't know the name of the key(s) with the
"<null>"
objects, then you have to iterate:To locate the
"<null>"
values, I'm using a string comparison since"<null>"
is a string in your sample. But I'm not sure if this is really the case.I have created a category for NSJSOn serialisation class.
Create a category and import class to use its methods...