12

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.

1
  • @MartinR if i try the following if (dict["outcome_status"] as NSString != "") it still crashes.
    – chirag90
    Commented Mar 25, 2015 at 19:50

6 Answers 6

27

Try something like this:

Swift code:

if let outcome = dict["outcome_status"] as? NSDictionary {
    //Now you know that you received a dictionary(another json doc) and is not 'nil'
    //'outcome' is only valid inside this if statement

    if let category = outcome["category"] as? String {
        //Here you received string 'category'
        outcomeStatusLabel.text = category
        outcomeStatusLabel.font = UIFont.systemFontOfSize(14.0)
        outcomeStatusLabel.numberOfLines = 0
    }

    if let date = outcome["date"] as? String {
        //Here you received string 'date'
        outcomeDateLabel.text = date
        outcomeDateLabel.font = UIFont.systemFontOfSize(14.0)
        outcomeDateLabel.numberOfLines = 0
    }
}

This is a safe way to work with Json.

6
  • 1
    Thank you, this works brilliantly, the app does not crash now.
    – chirag90
    Commented Mar 25, 2015 at 19:55
  • Awesome ... I hadn't been handling sad paths at all and my apps were crashing on sad path :/ Commented Dec 13, 2016 at 16:33
  • This example works with strings... but with other type doesn´t work. I think the best solution is @NunoFerro stackoverflow.com/a/37468020/4693765 Commented Apr 12, 2017 at 7:52
  • @JosePoseS You need pay attention into the code, there is a type cast operator (as?) that will try change json content to be a string (if not return nil), said that, if your json content is not a string you need change the type.
    – Vagner
    Commented Apr 12, 2017 at 13:01
  • @Vagner You need pay more attention to title question. "How to check if Json is null in swift?" not if is string or other type. I up vote too because is not wrong at all. Commented Sep 28, 2017 at 18:18
17
if ((nullObject as? NSNull) == nil)  {
        ...
       }
10

in case you are using Alamofire and JSONSubscriptType use this:

if !(parsedJSON["someKey"] == JSON.null) {
//do your stuff 
}
1

Try corresponding Swift class to following Objective C class,

dict["outcome_status"] != [NSNull null]
0

If you're using SwiftyJSON, just check .isEmpty() on the object to confirm if it's empty.

0

You can check json is null or not like this

if let data = response["data"].dictionary {
    // do something
}
else {
    // do something
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.