如何快速检查JSON是否为null? [英] How to check if JSON is null in swift?

查看:102
本文介绍了如何快速检查JSON是否为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个以以下格式带回json的应用程序

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

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

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

如果"outcome_status"具有值,则显示类别和日期,但是,如果"outcome_value"不具有任何值(在大多数情况下),则如下所示

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" =强制; month ="2015-01"; "outcome_status" ="; "persistent_id" =";

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

问题是如何检查result_status的值是否为"null"?

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

我已经尝试了以下方法来将类别和日期存储到标签中,但是它遇到了错误,第一个if语句应该检查该值是否不为null,如果不是,则转到next if语句.但是它继续到下一个if语句,并且出现以下错误

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

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

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语句,则它仅在"outcome_status" ="null"时崩溃,并且如果"outcome_status"中有某些值,则可以正常工作

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"

我需要怎么做,如果值是= null,它会在if语句第1处停止?

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

谢谢.

推荐答案

尝试如下操作:

快捷代码:

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
    }
}

这是与Json合作的安全方法.

This is a safe way to work with Json.

这篇关于如何快速检查JSON是否为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆