iOS NSDictionary value determine if Boolean from JSON boolean - ios

I have a JSON response from a web server that looks like this:
{"success":true, "token":"123456"}
and I want to use that in an if statement, and compare it with "YES".
However, doing this doesn't work:
NSDictionary *response = [response JSONValue]; // the JSON value from webservice response, converted to NSDictionary
if ([response objectForKey:#"success"]){} // does not work
if ([response objectForKey:#"success"] == YES){} // does not work
if ([[response objectForKey:#"success"] integerValue] == YES) {} // does not work...erroneous probably
How can I work around this? Typecasting in Boolean yields a warning too

since [response objectForKey:#"success"] does not work, what happens when you try [response valueForKey: #"success"]?
I suspect it returns a NSNumber and then you can do something like:
NSNumber * isSuccessNumber = (NSNumber *)[response objectForKey: #"success"];
if([isSuccessNumber boolValue] == YES)
{
// this is the YES case
} else {
// we end up here in the NO case **OR** if isSuccessNumber is nil
}
Also, what does NSLog( #"response dictionary is %#", response ); look like in your Console? I see the JSON library you're using does return NSNumber types for objectForKey, so I suspect you might not have a valid NSDictionary.

An alternative approach to this, which requires no conversion to NSNumber is something like below:
if ([response objectForKey:#"success"])
{
if ([[response objectForKey:#"success"] boolValue])
NSLog(#"value is true");
}

Related

How to check if json object contains <null>?

I am getting a Json from server by making a network request in my app.I am getting <null> value for some keys in Json object.My app gets crashed if this type of response is received.Please tell me how can i validate>?
I have tried this but it does not work all time.
if(!(user_post.username==(id)[NSNull null]) )
{
user_post.username=[dict_user_info objectForKey:#"name"];
if(user_post.username!=nil)
{
ser_post.username=[dict_user_info objectForKey:#"name"];
}
else
{
user_post.username=#"Username";
}
}
Consider testing the value for null so your program won't crash. Like this:
if([dict_user_info objectForKey:#"name"] != [NSNull null])
{
ser_post.username=[dict_user_info objectForKey:#"name"];
}
Create a Category of NSDictionary and add following method in it, which replaces null value with empty string for each key in dictionary.
- (NSDictionary *)dictionaryByReplacingNullsWithStrings
{
const NSMutableDictionary *replaced = [self mutableCopy];
const id nul = [NSNull null];
const NSString *blank = #"";
for(NSString *key in self) {
const id object = [self objectForKey:key];
if(object == nul || object == NULL) {
//pointer comparison is way faster than -isKindOfClass:
//since [NSNull null] is a singleton, they'll all point to the same
//location in memory.
[replaced setObject:blank
forKey:key];
}
}
return [replaced copy];
}
Usage :
[yourJSONDictionary dictionaryByReplacingNullsWithStrings];
Read more about Category in iOS Tutorial 1 and Tutorial 2
yourJsonObject = [myDic valueforkey#"key"];
if(yourJsonObject != [NSNull null])
{
//not null
}
** you can also check whether object exist or not
if(yourJsonObject)
{
//exist
}
I think you've confused your logic. I am trying to stay true to your code, but let me know if the following is not what you intended:
if (dict_user_info[#"name"] != nil && [dict_user_info[#"name"] isKindOfClass:[NSNull class]] == NO) {
user_post.username = dict_user_info[#"name"];
if (user_post.username != nil) {
ser_post.username = user_post.username;
} else {
user_post.username = #"Username";
}
}
These are a couple of methods I wrote for my projects, try them :
/*!
* #brief Makes sure the object is not NSNull or NSCFNumber, if YES, converts them to NSString
* #discussion Sometimes JSON responses can contain NSNull objects, which does not play well with Obj-C. So when you access a value from a JSON and expect it to be an NSString, pass it through this method just to make sure thats the case.
* #param str The object that is supposed to be a string
* #return The object cleaned of unacceptable values
*/
+ (NSString *)cleanedJsonString:(id)str
{
NSString *formattedstr;
formattedstr = (str == [NSNull null]) ? #"" : str;
if ([str isKindOfClass:[NSNumber class]]) {
NSNumber *num = (NSNumber*) str;
formattedstr = [NSString stringWithFormat:#"%#",num];
}
return formattedstr;
}
/*!
* #brief Makes Sure the object is not NSNull
* #param obj Sometimes JSON responses can contain NSNull objects, which does not play well with Obj-C. So when you access a value from a JSON ( NSArray, NSDictionary or NSString), pass it through this method just to make sure thats the case.
* #return The object cleaned of unacceptable values
*/
+ (id)cleanedObject:(id)obj
{
return (obj == [NSNull null]) ? nil : obj;
}
/*!
* #brief A JSON cleaning function for NSArray Objects.
* #discussion Sometimes JSON responses can contain NSNull objects, which does not play well with Obj-C. So when you access a value from a JSON and expect it to be an NSArray, pass it through this method just to make sure thats the case. This method first checks if the object itself is NSNull. If not, then it traverses the array objects and cleans them too.
* #param arr The Objects thats supposed to be an NSArray
* #return The NSNull Cleaned object
*/
+ (NSArray *)cleanedJsonArray:(id)arr
{
if (arr == [NSNull null]) {
return [[NSArray alloc] init];
}
else
{
NSMutableArray *arrM = [(NSArray*)arr mutableCopy];
int i=0;
for (id __strong orb in arrM)
{
if (orb == [NSNull null])
{
[arrM removeObjectAtIndex:i];;
}
i++;
}
return arrM;
}
}
Just pass a JSON string, array or object to the appropriate method and the method will clean it for you.
Do yourself a favour and write a method that handles this and put it into an extension. Like
- (NSString*)jsonStringForKey:(NSString*)key
{
id result = self [key];
if (result == nil || result == [NSNull null]) return nil;
if ([result isKindOfClass:[NSString class]]) return result;
NSLog (#"Key %#: Expected string, got %#", key, result);
return nil;
}
You might even add some code that accepts NSNumber* results and turns them into strings, if that is what your server returns (some poster here had the problem that his server returned dress sizes as numbers like 40 or strings like "40-42" which makes something like this useful).
And then your code becomes one readable line
user_post.username = [dict_user_info jsonStringForKey:#"name"] ?: #"username";
I actually use several slightly different methods depending on whether I expect null, expect no value, expect an empty string or not, which gives me warnings when my assumptions are wrong (but always returns something that doesn't break).
try this:
if(!(user_post.username == (NSString *)[NSNull null]) )

ios id doesn't convert to nsinteger

I have the following response that returns from a block:
response: {
error = "";
success = 1;
}
I attempt to evaluate "success" but it never evaluates as equal to 1 only as "else":
NSLog(#"response: %#", responseObject);
NSInteger success = (NSInteger)responseObject[#"success"];
NSString *errorMessage = (NSString *)responseObject[#"error"];
if (success == 0) {
NSLog(#"success is false");
NSLog(#"JSON: %#", errorMessage);
saveCallback(NO, [self createErrorFromDescription:errorMessage]);
}else if (success == 1){
NSLog(#"success is true");
saveCallback(YES, nil);
}else{
NSLog(#"success is else");
NSLog(#"JSON: %#", errorMessage);
saveCallback(NO, [self createErrorFromDescription:errorMessage]);
}
What am I doing wrong?
NSInteger is a primitive and id is an object (actually a pointer to an object), in this case likely an NSNumber.
Directly casting the pointer to an object, to NSInteger will not transform it into a integer value type, it will just reinterpret the pointers memory as an integer.
To transform the number object to a integer value you would call integerValue on it.
(It could also be that the number is missing from the response or it could be returned as the NSNull object, hence the error checking below)
NSNumber *successNumber = responseObject[#"success"];
NSInteger success;
if (!successNumber || successNumber == [NSNull null]) {
// the response doesn't contain anything for the "success" key :(
}
// if successNumber is nil, this will be 0.
// if successNumber is the NSNull object, this will crash (unrecognized selector)
// Just be aware of both of those.
success = [successNumber integerValue];
NSInteger success = [responseObject[#"success"] integerValue];
Given that it appears you're using raw json, you need to be very careful that you don't use a NULL value; this will result in an exception.
Since there are many types of Null in Objective C, it's best to use class introspection to make sure your objects are valid.
NSDictionary *responseArray = responseObject;
NSInteger success=0;
if([responseObject isKindOfClass:[NSDictionary class]])
{
// responseObject is safe to subscript
NSNumber *successNumber = responseObject[#"success"];
// Use introspection; messaging nil doesn't cause an exception and returns nil
if ([successNumber isKindOfClass:[NSNumber class]])
{
// NSInteger is a primitive
success = [successNumber integerValue];
}
}
// If success is anything but zero, assume it's true
if (success)
{
NSLog(#"success is true");
}
else
{
NSLog(#"success is false");
}
Presumably, your success key is either 1 or 0, so you could simplify this code a bit. In general, though, this is how you'll want to deal with objects that may be NULL rather than simply nil.

IF statement issue in IOS using NSString

My if statement won't work. active returns 1 but will not work in the IF statement
JSONDecoder *jsonKitDecoder = [JSONDecoder decoder];
NSDictionary *dict = [jsonKitDecoder objectWithData:jsonData];
NSString *userid = [dict valueForKeyPath:#"users.user_id"];
NSString *active = [dict valueForKeyPath:#"users.active"];
NSLog(#"%#",userid); // 2013-06-20 03:03:21.864 test[81783:c07] (74)
NSLog(#"%#",active); // 2013-06-20 03:03:21.864 test[81783:c07] (1)
if ([active isEqualToString:#"1"]){
// Do something
}
I can't seem to get this IF to work. Do I need to change the NSString to a int?
For starters, use a modern style for retrieving values from dictionaries, rather than valueForKeyPath:.
NSDictionary* users = dict[#"users"];
id active = users[#"active"];
Once you're using a modern style, my guess is that the active value is actually an NSNumber representing a boolean value. So your if block would read:
if([active isKindOfClass:NSNumber] && [active boolValue]) {
//active is an NSNumber, and the user is active
}
The syntax of your if statement is just fine. I would try the alternate method for retrieving values from a dictionary as mentioned above.
NSString *active = #"1";
if ([active isEqualToString:#"1"])
{
// Do something
NSLog(#"It works!");
}
More than likely the "users.active" object being returned from that NSDictionary-ized JSON stream is a "BOOL" or a "NSInteger" as the payload of a NSNumber object and it's not a NSString object.
Try using:
NSNumber * activeNumber = [dict valueForKeyPath: #"users.active"];
and see if "if ([activeNumber boolValue] == YES)" works better for you.

How do I get the datatype of the value given the key of NSDictionary?

I have a NSDictionary and what to get the datatype for value given the key
is it possible?
You could check the variants you accept by
id objectValue = [dictionary valueForKey:#"SomeKey"];
if ([objectValue isKindOfClass:[NSString class]]) {
//Object is a NSString
} else if ([objectValue isKindOfClass:[NSArray class]]) {
//Object is a NSArray
} else if ([objectValue isKindOfClass:[NSDictionary class]]) {
//Object is a NSDictionary
} else if ([objectValue isKindOfClass:[NSNumber class]]) {
//Object is a NSNumber
}
And so on.. In this pattern just handle all the types your app supports. Ignore values your app doesn't support by this pattern or just fail gracefully in another way when you don't support the datatype of the value
To just figure out what class it is (to debug the application for example) you can do:
NSString *className = NSStringFromClass([objectValue class]);
you could use NSStringFromClass to get the type, or failing that you could use isKindOfClass:
NSDictionary *dictionary = #{
#"string": #"Something",
#"number": #(1),
#"null": [NSNull null],
#"custom": [[CustomType alloc] init]
};
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(#"type = %#", NSStringFromClass([obj class]));
}];
output:
type = CustomType
type = NSNull
type = __NSCFNumber
type = __NSCFConstantString

NSNull value inside a object generated by JSONKit (or similar)

I'm storing the result of JSONKit parse in a key/value database (LevelDB), but the JSON I'm downloading has some filed set to null, and this won't let you serialize the corresponding generated object (NSArray or NSDictionary), to store it as an object.
¿Any idea how can I deep iterate over a NSSomething (Dictionary or Array) to change those values?
There is a post https://github.com/johnezang/JSONKit/issues/25 that explains how to modify the framework to omit it from dictionaries and array's
Sometime it happened to me that a null value was set to something what was not recognized neither as NSNull nor as NSString. Therefore i´ve replaced all null strings in the json string before i parse it with NSJSONSerialization. I´ve read the data into a NSData object, copied it into a NSString object, replaced the null strings and copied it again into a NSData object as NSJSONSerialization expects a NSData object. Maybe you can write it shorter but it works.
Here´s the code
NSString *jsonPath = [myPath stringByAppendingPathComponent:#"myDataFile.json"];
NSMutableData *myJSON = [[NSMutableData alloc] initWithContentsOfFile:jsonPath];
NSString *jsonString = [[NSString alloc] initWithBytes:myJSON.bytes length:myJSON.length encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"null" withString:#"\"placeholder\""];
NSData * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonParsingError];
after that all previous null occurances will contain a placeholder string.
I parsed the JSON to a mutableObject [[JSONDecoder decoder] mutableObjectWithUTF8String:(const unsigned char *) [json UTF8String] length:[json lengthOfBytesUsingEncoding:NSUTF8StringEncoding] error:nil ]; and then used this code to fix it:
-(id) fixObject: (id) a
{
if([a isKindOfClass:[NSMutableDictionary class]])
{
NSMutableDictionary *ad = a;
for (NSObject *key in ad)
{
if([[ad objectForKey:key] isKindOfClass:[NSMutableDictionary class]] || [[ad objectForKey:key] isKindOfClass:[NSMutableArray class]])
[self fixObject:[ad objectForKey:key]];
else
{
if((NSNull *)[ad objectForKey:key] == [NSNull null]) [ad setObject:#"" forKey:key];
}
}
}else if([a isKindOfClass:[NSMutableArray class]])
{
NSMutableArray *ar = a;
for (NSObject *ob in ar)
{
if([ob isKindOfClass:[NSMutableDictionary class]] || [ob isKindOfClass:[NSMutableArray class]])
{
[self fixObject:ob];
}
else if((NSNull *)ob == [NSNull null])
{
[ar removeObject:ob];
}
}
}
return a;
}
If you find a better way to do this, let me know!

Resources