ios 如何快速检查JSON是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29264449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if JSON is null in swift?
提问by chirag90
I am currently working on an app which brings back json, in the following format
我目前正在开发一个应用程序,它以以下格式带回 json
"location_subtype" = "somevalue"; "location_type" = Force; month = "2015-01"; "outcome_status" = { category = "somevalue"; date = "somevalue"; };
"location_subtype" = "somevalue"; “location_type” = 力;月 = "2015-01"; “结果状态”= {类别=“某个值”;日期=“某个值”;};
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
如果“outcome_status”有值,它会显示类别和日期,但是如果“outcome_value”没有任何值(在大多数情况下),则如下所示
"location_subtype" = "somevalue"; "location_type" = Force; month = "2015-01"; "outcome_status" = ""; "persistent_id" = "";
"location_subtype" = "somevalue"; “location_type” = 力;月 = "2015-01"; “结果状态”=“”;"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
我尝试了以下将类别和日期存储到标签中,但遇到错误,第一个 if 语句应检查该值是否为空,以及是否不转到下一个 if 语句。但是它继续下一个 if 语句,我收到以下错误
Thread 1: EXC_BAD_ACCESS(code=2, address=0x102089600)
线程 1:EXC_BAD_ACCESS(代码=2,地址=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"
如果我删除第一个 if 语句,它只会在“outcome_status”=“null”时崩溃,并且如果“outcome_status”中有一些值,它就可以正常工作
What do i need to do so it stops at 1st if statement if the value is = null?
如果值为 = null,我需要做什么才能在第一个 if 语句处停止?
Thank you in advance.
先感谢您。
回答by Vagner
Try something like this:
尝试这样的事情:
Swift code:
SWIFT代码:
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.
这是使用 Json 的一种安全方式。
回答by Nuno Ferro
if ((nullObject as? NSNull) == nil) {
...
}
回答by Alex Delgado
in case you are using Alamofire and JSONSubscriptType use this:
如果您使用的是 Alamofire 和 JSONSubscriptType,请使用:
if !(parsedJSON["someKey"] == JSON.null) {
//do your stuff
}
回答by sleepwalkerfx
Try corresponding Swift class to following Objective C class,
将对应的 Swift 类尝试到以下 Objective C 类,
dict["outcome_status"] != [NSNull null]