当值为null且有些数字时,如何在label中显示字符串值

时间:2013-07-06 06:28:06

标签: iphone ios nsstring uilabel

我有 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”为null时,我想在“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”为空时,我在标签中显示null。

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

如何解决这个问题...... 谢谢。

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

试试这个:

 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

答案 2 :(得分:1)

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

你必须写两个递归调用的函数。

像这样打电话......

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

这是Dictionary的功能。

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

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

答案 3 :(得分:0)

if([currentEmplyCellDit objectForKey:@"total"] == [NSNull null]) 
{
  yourLbl.text=@"0";
}
else
{
  yourLbl.text = [currentEmplyCellDit objectForKey:@"total"]
}