简体   繁体   English

如何处理Web服务的空响应

[英]How to handle null response of web services

My app consuming PHP web services. 我的应用程序使用PHP Web服务。 But some field of response is null and i cant handle that null value. 但是某些响应字段为null,我无法处理该null值。

This is my response 这是我的回应

[
{
ID: 1,
ChapterID: 0,
Code: "FLGSV01",
Number: "0",
Text: "Swedish Flag",
VersionChanged: 1,
LastChange: "Jun 26 2013 12:00:00:000AM",
Type: "img",
info: null
}
]

I have parsed this response in my dictionary which like this 我已经在我的字典中解析了这个回复,就像这样

tmpDictn : {
    ID = 1,
    ChapterID = 1;
    Code = SECH1W;
    ID = 2;
    LastChange = "Jun 26 2012 12:00:00:000AM";
    Number = 001;
    Text = "ett
\n";
    Type = img;
    VersionChanged = 1;
    info = "<null>";
}

Now as you see value for info is Null and if i try to parse it in string than my string will be <null> but instead of this i just want it to display empty string. 现在,如您所见,信息值是Null,如果我尝试以字符串形式解析它,则我的字符串将是<null>但我只希望它显示空字符串,而不是它。

I have tried this code but no luck 我已经尝试过此代码,但没有运气

if ([tmpDictn objectForKey:@"info"]!=nil) {
            info = [tmpDictn objectForKey:@"info"];
        }

And this code gives me error of NSNull 这段代码给了我NSNull错误

if ([[tmpDictn objectForKey:@"info"] isEqualToString:@"<null>"]) {
            info = [tmpDictn objectForKey:@"info"];
        }

So how could i handle this null value? 那么我该如何处理这个空值呢?

Compare with [NSNull null] [NSNull null]比较

From Docs 文档

The NSNull class defines a singleton object used to represent null values in collection objects (which don't allow nil values). NSNull类定义一个单例对象,该对象用于表示集合对象中的空值(不允许使用nil值)。

if([tmpDictn objectForKey:@"info"] == [NSNull null]) 
{
  info = nil;
}
else
{
  info = [tmpDictn objectForKey:@"info"]
}
info = [tmpDictn objectForKey:@"info"];
if(info == null)
{
    //Handle null
}
if ([[tmpDictn objectForKey:@"info"] isKindOfClass:[NSNull class]])
{
    info = [tmpDictn objectForKey:@"info"];
}

Try to use [NSNull null] 尝试使用[NSNull null]

if ([tmpDictn objectForKey:@"info"]!=[NSNull null]) {
      info = [tmpDictn objectForKey:@"info"];
} else {
      //Handle null value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM