当值为null或某个数值时,如何在标签中显示字符串值

时间:2020-12-14 11:50:35

I have JSON object like this.

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

I want to display JSON string "total" in label. When "total" is null i want to display '0' when "total" is some number i want to display that number in same label.

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

Here i`m trying this code

我在这里尝试这个代码

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];

this code displaying correctly but i shows null in label when "total" is null.

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

I want to display 0 when "total" is NULL

当“total”为空时,我想显示0

How to Solve this... Thanks.

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

4 个解决方案

#1


2  

You can make condition to check null in your string and then replace this with whatever you want.

您可以使条件在您的字符串中检查null,然后用您想要的任何东西替换它。

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

#2


1  

Try this :

试试这个:

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

And remember this thing :

记住这一点:

   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

and see my answer Checking a null value from Json response in Objective-C

查看我的答案,检查Objective-C中的Json响应的空值

#3


1  

Just call one of these function using the dictionary or array whatever you are receiving from the JSON. These functions will remove all NULL values from your response.

只要使用字典或数组调用其中一个函数,就可以从JSON接收到任何内容。这些函数将从响应中删除所有空值。

You have to write both functions they are calling each other recursively.

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

Call like these...

这样的电话……

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

This is the function for 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;
}

This is the function for array

这是数组的函数

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

This will definitely help you. Must try.....

这肯定对你有帮助。必须努力.....

#4


0  

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

#1


2  

You can make condition to check null in your string and then replace this with whatever you want.

您可以使条件在您的字符串中检查null,然后用您想要的任何东西替换它。

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

#2


1  

Try this :

试试这个:

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

And remember this thing :

记住这一点:

   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

and see my answer Checking a null value from Json response in Objective-C

查看我的答案,检查Objective-C中的Json响应的空值

#3


1  

Just call one of these function using the dictionary or array whatever you are receiving from the JSON. These functions will remove all NULL values from your response.

只要使用字典或数组调用其中一个函数,就可以从JSON接收到任何内容。这些函数将从响应中删除所有空值。

You have to write both functions they are calling each other recursively.

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

Call like these...

这样的电话……

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

This is the function for 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;
}

This is the function for array

这是数组的函数

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

This will definitely help you. Must try.....

这肯定对你有帮助。必须努力.....

#4


0  

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