【问题标题】:How to display string value in label when the value is null and some number当值为空和某个数字时如何在标签中显示字符串值
【发布时间】:2013-07-06 06:28:06
【问题描述】:

我有这样的 JSON 对象。

     {
        "id": "1",
        "subTotal": "20000.00",
        "total": "124565.00",
        "timeOfTrans": "3555525",
        "status": "1",
        "name": "gjgjgf",
        "email": "a@a.com",
        "level": "gjfgj",
        "teamId": "1"
    },
    {
        "id": "2",
        "subTotal": null,
        "total": null,
        "timeOfTrans": null,
        "status": null,
        "name": "Rajendra",
        "email": "b@b.com",
        "level": "gfjgfjg",
        "teamId": "1"
    }

我想在标签中显示 JSON 字符串“total”。当“total”为空时,我想在“total”是某个数字时显示“0”,我想在同一个标​​签中显示该数字。

我正在尝试这段代码

totalLbl=(UILabel *)[cell viewWithTag:1];
id total = [currentEmplyCellDit objectForKey:@"total"];
if ([total isKindOfClass:(id)[NSNull null]])
{
    //Your value of total is NULL
    totalLbl.text = @"0";
}
//Show the Value.
totalLbl.text = [NSString stringWithFormat:@"%@",total];

此代码显示正确,但当“total”为空时,我在标签中显示空。

当“total”为NULL时我想显示0

如何解决... 谢谢。

【问题讨论】:

  • 检查我更新的答案好友..

标签: iphone ios nsstring uilabel


【解决方案1】:

您可以设置条件来检查字符串中的 null,然后将其替换为您想要的任何内容。

id yourValue=[youJSONDict objectForKey:@"total"];;
if ([yourValue isKindOfClass:[NSNull class]]) {
    yourLabel.text=@"0";
}
else{
    yourLabel.text=[NSString stringWithFormat:@"%@",yourValue];
}

【讨论】:

  • 我在表格视图中显示标签。
  • 当“total”值为NULL时显示。当“总”值为 NULL 时,我想显示 0。
【解决方案2】:

试试这个:

 if([dictionary valueForKey:@"total"] != nil) {
      // The key existed..
 }
 else {
       // No joy...
 }

记住这件事 :

   objectForKey will return nil if a key doesn't exist


  Symbol    Value            Meaning
  =======   =============   =========================================
   NULL     (void *)0       literal null value for C pointers
   nil      (id)0           literal null value for Objective-C objects
   Nil      (Class)0        literal null value for Objective-C classes
  NSNull    [NSNull null]   singleton object used to represent null

看看我的回答Checking a null value from Json response in Objective-C

【讨论】:

    【解决方案3】:

    只需使用字典或数组调用这些函数之一,无论您从 JSON 接收什么。这些函数将从您的响应中删除所有 NULL 值。

    您必须编写它们以递归方式相互调用的两个函数。

    像这样打电话...

            NSMutableDictionary * ResponseDict =(NSMutableDictionary *)[responseString JSONValue];
            ResponseDict = [self removeNullFromDictionary:ResponseDict];
            NSLog(@"ResponseDict = %@",ResponseDict);
    

    这是字典的功能。

    -(NSMutableDictionary *)removeNullFromDictionary : (NSMutableDictionary *)dict
    {
    
        for (NSString * key in [dict allKeys])
        {
            if ([[dict objectForKey:key] isKindOfClass:[NSNull class]])
            {
                [dict setValue:@"" forKey:key];
            }
            else if ([[dict objectForKey:key] isKindOfClass:[NSMutableDictionary class]]||[[dict objectForKey:key] isKindOfClass:[NSDictionary class]])
            {
                [dict setObject:[self removeNullFromDictionary:[dict objectForKey:key]] forKey:key];
            }
            else if ([[dict objectForKey:key] isKindOfClass:[NSMutableArray class]]||[[dict objectForKey:key] isKindOfClass:[NSArray class]])
            {
                [dict setObject:[self removeNullFromArray:[dict objectForKey:key]] forKey:key];
            }
    
        }
    
        return dict;
    }
    

    这是数组的函数

    -(NSMutableArray *)removeNullFromArray : (NSMutableArray *)arr
    {
        for (int cnt = 0; cnt<[arr count]; cnt++)
        {
            if ([[arr objectAtIndex:cnt] isKindOfClass:[NSNull class]])
            {
                [arr replaceObjectAtIndex:cnt withObject:@""];
            }
            else if ([[arr objectAtIndex:cnt] isKindOfClass:[NSMutableDictionary class]]||[[arr objectAtIndex:cnt] isKindOfClass:[NSDictionary class]])
            {
                [arr replaceObjectAtIndex:cnt withObject:[self removeNullFromDictionary:(NSMutableDictionary *)[arr objectAtIndex:cnt]]];
            }
            else if ([[arr objectAtIndex:cnt] isKindOfClass:[NSMutableArray class]]||[[arr objectAtIndex:cnt] isKindOfClass:[NSArray class]])
            {
                [arr replaceObjectAtIndex:cnt withObject:[self removeNullFromArray:(NSMutableArray *)[arr objectAtIndex:cnt]]];
            }
        }
        return arr;
    }
    

    这肯定会对您有所帮助。一定要试试.....

    【讨论】:

      【解决方案4】:
      if([currentEmplyCellDit objectForKey:@"total"] == [NSNull null]) 
      {
        yourLbl.text=@"0";
      }
      else
      {
        yourLbl.text = [currentEmplyCellDit objectForKey:@"total"]
      }
      

      【讨论】:

      • 欢迎来到 StackOverflow!如果您编辑答案并添加一些解释为什么此代码回答问题会很好。仅仅粘贴一段代码并没有多大帮助。
      猜你喜欢
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 2022-11-22
      • 2017-11-13
      • 2023-04-07
      相关资源
      最近更新 更多