NSJSONSerialization returns "<null>" string - ios

I'm try to set an NSDictionary to a JSON object retrieved from the server, I'm doing that in this line:
_peopleArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
It works fine and properly creates the dictionary. However, I have a problem, values that are null in the JSON object are stored as "<null>" string values in the dictionary. Is there any way to fix this or work around it? I want to avoid traversing through the entire thing and setting them to #"".
Thanks for any help!
~Carpetfizz

You are wrong. null values in JSON are not stored as <null> string values. They are stored as NSNull objects, which NSLog logs as <null>.
You can never trust what data you were given. If you assume you got an NSString and the server sends you a number, your code is likely to crash.
NSString* myJSONString = ...;
if ([myJSONString isKindOfClass:[NSString class]]) {
it is a string
} else {
it is not a string
}

There is nothing I guess, though it can be easily corrected from api makers, if not possible, you can always put a simple macro, I use to avoid such thing, follow macro below
#define Is_Empty(value) (value == (id)[NSNull null] || value == nil || ([value isKindOfClass:[NSString class]] && ([value isEqualToString:#""] || [value isEqualToString:#"<null>"]))) ? YES : NO
#define IfNULL(original, replacement) IsNULL(original) ? replacement : original
#define IsNULL(original) original == (id)[NSNull null]
#define SafeString(value) IfNULL(value, #"")
Usage
self.label.text=SafeString([dic objectForKey:#"name"]);

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]) )

Correct NSString output

I have talbeView that populate data from JSON. One my text label fill itself with NSString, and sometimes it output 0(NSNULL), which obviously, not suppose to be output on cell text label.
Following code:
if ([(cell.myNameLabel.text) isEqual: #"0(NSNull)"]){
cell.myNameLabel.text = #"No Name";
}
Isn't work, i wonder why?
How to fix that?
Data populated from JSON use here like this :
//use accordingly that it may contain either NSDictionary or NSArray.
NString *strValue = youJSONDataArray[indexPath.row];
if ([value isEqual:[NSNull null]] || value.length == 0) {
strValue == #"";
}
Set string value in UILabel like this:
cell.myNameLabel.text = strValue;
NSJSONSerialization uses NSNull to indicate a null JSON value.
Try this instead:
if ([cell.myNameLabel.text isEqual:[NSNull null]]) {
// …
}
But you'll probably want to strip out the NSNulls during parsing so that your code isn't littered with NSNull checks.
If a JSON response has a null value then its indicated by NSNull by NSJSONSerialization class so you can modify your code to
if ([cell.myNameLabel.text isEqual:[NSNull null]]) {
// your logic here
}

NSNumber with IF statement issue

I am loading data from a server but I have an issue that the value that I am returning is zero(0) while I can't go inside if. Please where would be the problem?
-(void)method1
{
NSNumber *value = [data objectForKey:#"samount"];
NSLog(#"number is -%#-", value); //number is -0-
if (value == 0)
{
NSLog(#" OK :) ");
}
else
{
NSLog(#" Bad :( ");
}
}
Use isEqual:
if ([value isEqual:#(0)])
That will also evaluate correctly in case value is nil (where == comparison with floatValue or similar methods would fail)
value is an object, and more precisely a NSString object (as per your comments in Alladinian's answer), but you are checking its address. You can convert your string to NSNumber with NSNumberFormatter and then check its value or rely on NSString's built-in methods: integerValue, floatValue, etc.
Assuming value is a NSNumber/NSString:
if ([value integerValue] == 0)
See Getting Numeric Values in NSString documentation and Accessing Numeric Values in the NSNumber documentation and pick the method that best suits your data type.

ios: Compare NSString to "<null>" not working

I am consuming a web service that returns JSON. One of the values I get is "< null >".
When I run the follwoing code the if statment still get executed when it is not suppposed to.
Any reason why?
NSDictionary *location = [dictionary valueForKey:#"geoLocation"]; //get the product name
NSString *latitude = [location valueForKey:#"latitude"];
NSLog(#"%#", latitude);
NSString *longitude = [location valueForKey:#"longitude"];
if (![latitude isEqual: #"<null>"] && ![longitude isEqual: #"<null>"]) {
NSLog(#"%d", i);
CLLocationCoordinate2D coordinate;
coordinate.longitude = [latitude doubleValue];
coordinate.longitude = [longitude doubleValue];
[self buildMarketsList:coordinate title:title subtitle:nil]; //build the browse list product
}
I am consuming a web service that returns JSON. One of the values I get is "< null >"
Aha. Two possibilities:
I. The JSON doesn't contain latitude and longitude information. In this case, the keys for them aren't present in the dictionary you're getting back, so you are in fact obtaining a nil (or NULL) pointer. As messaging nil returns zero, both conditions will fire (due to the negation applied). Try this instead:
if (latitude != nil && longitude != nil)
and never rely on the description of an object.
II. Probably the JSON contains null values, and the JSON parser you're using turns null into [NSNull null], and in turn you're trying to compare a string against that NSNull. In this case, try this:
if (![latitude isEqual:[NSNull null]] && ![longitude isEqual:[NSNull null]])
I had same Problem, But solved as below,
Replace your if with following,
if (![latitude isKindOfClass:[NSNull class]] && ![longitude isKindOfClass:[NSNull class]])
if ([latitude isEqual:[NSNull null]])
{
//do something
latitude = #"";
}
else
{
//do something else
latitude = json value
}
This is what I will do. This is because I need to store the value even if a null return.
Typically if you get null in a JSON response, you'll need to check against NSNull.
In your case, you should do something like this:
if ( [location valueForKey:#"longitude"] == [NSNull null]) {
// is null object
}

iOS can't check if object is null

So I have the following code:
- (IBAction)doSomething
{
if (txtName.text != (id)[NSNull null] || txtName.text.length != 0 ) {
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#", txtName.text];
[lblMessage setText:msg];
}
}
txtName is an UITextField, what I'm doing wrong? I'm trying to display some text only when the user types something in the box.
Best Regards,
Text in a text field is a NSString instance or nil value, it is never equal to the instance of NSNull class (which is not the same as nil). So as 1st comparison is always true then the whole if-condition evaluates to true and message appears.
You could correct your if condition to
if (txtName.text != nil && txtName.text.length != 0 )
or, as sending length message to the nil will return 0 anyway just have
if (txtName.text.length != 0 )
although I usually use the 1st option with 2 comparisons
if (txtName.text != (id)[NSNull null] || txtName.text.length != 0 ) {
Read it as "If the text is null or the length is not 0"
txtName.text is never nil (you can just compare against nil for a null check, by the way) - the text box always holds some text, even if it's empty. So the first disjunct is always true, and the box will always appear.
I got the same problem like you. This problem relates to NSNull class. And here is my code to check object is null.
NSString* text;
if([text isKindOfClass:[NSNull class]])
{
//do something here if that object is null
}
Hope this can help.
#define SAFESTRING(str) ISVALIDSTRING(str) ? str : #""
#define ISVALIDSTRING(str) (str != nil && [str isKindOfClass:[NSNull class]] == NO)
#define VALIDSTRING_PREDICATE [NSPredicate predicateWithBlock:^(id evaluatedObject, NSDictionary *bindings) {return (BOOL)ISVALIDSTRING(evaluatedObject);}]
SAFESTRING(PASS_OBJECT OR STRING);
Solution found! ![txtName.text isEqualToString:#""]
- (IBAction)doSomething
{
if (![txtName.text isEqualToString:#""]){
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#", txtName.text];
[lblMessage setText:msg];
}
}
Please try this:
when value of myObj is nil or < null>
if([myObj isEqual:[NSNull class]] || !myObj) {
// your code
}

Resources