Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if JSON is null in swift?

I am currently working on an app which brings back json, in the following format

"location_subtype" = "somevalue"; "location_type" = Force; month = "2015-01"; "outcome_status" = { category = "somevalue"; date = "somevalue"; };

if the "outcome_status" has value it shows category and date however if the "outcome_value" does not have any value(which is in most of the cases) it is shown as below

"location_subtype" = "somevalue"; "location_type" = Force; month = "2015-01"; "outcome_status" = ""; "persistent_id" = "";

The question is how can i check if the value for outcome_status is not "null"?

i have tried the following to store the category and date into label but it runs into error, the first if statement should check if the value is not null and if it is not go to next if statement. However it continues to next if statement and i get the following error

Thread 1: EXC_BAD_ACCESS(code=2, address=0x102089600)

if (dict["outcome_status"] != nil)
        {
            if ((dict["outcome_status"]as NSDictionary)["category"] != nil)
            {
                outcomeStatusLabel.text = ((dict["outcome_status"]as NSDictionary)["category"] as NSString)
                outcomeStatusLabel.font = UIFont.systemFontOfSize(14.0);
                outcomeStatusLabel.numberOfLines = 0
            }

            if ((dict["outcome_status"]as NSDictionary)["date"] != nil)
            {
                outcomeDateLabel.text = ((dict["outcome_status"]as NSDictionary)["date"] as NSString)
                outcomeDateLabel.font = UIFont.systemFontOfSize(14.0);
                outcomeDateLabel.numberOfLines = 0
            }
        }

If i remove the 1st if statement it only crashes when "outcome_status" = "null" and works perfectly fine if there some value in "outcome_status"

What do i need to do so it stops at 1st if statement if the value is = null?

Thank you in advance.

like image 774
chirag90 Avatar asked Mar 25 '15 19:03

chirag90


People also ask

How do you check null in Swift?

In Swift, you can also use nil-coalescing operator to check whether a optional contains a value or not. It is defined as (a ?? b) . It unwraps an optional a and returns it if it contains a value, or returns a default value b if a is nil.

Is null in JSON?

Null valuesJSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.

How do you handle null values in JSON response in Swift?

If the value of "id" is null in our JSON, parseRepository returns nil . If "id" is a string, parseRepository returns nil . If "id" is an object, parseRepository returns nil . And if "id" is an integer, we'll continue parsing our JSON and eventually return a Repository if all goes well.


2 Answers

in case you are using Alamofire and JSONSubscriptType use this:

if !(parsedJSON["someKey"] == JSON.null) {
//do your stuff 
}
like image 137
Alex Delgado Avatar answered Oct 15 '22 19:10

Alex Delgado


if ((nullObject as? NSNull) == nil)  {
        ...
       }
like image 23
Nuno Ferro Avatar answered Oct 15 '22 19:10

Nuno Ferro