3

我有这样的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”。当“总计”为空时,我想在“总计”是某个数字时显示“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 时,我想显示 0

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

4

4 回答 4

2

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

id yourValue=[youJSONDict objectForKey:@"total"];;
if ([yourValue isKindOfClass:[NSNull class]]) {
    yourLabel.text=@"0";
}
else{
    yourLabel.text=[NSString stringWithFormat:@"%@",yourValue];
}
于 2013-07-06T06:32:32.547 回答
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

于 2013-07-06T06:41:42.800 回答
1

只需使用您从 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;
}

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

于 2013-07-06T06:50:53.923 回答
0
if([currentEmplyCellDit objectForKey:@"total"] == [NSNull null]) 
{
  yourLbl.text=@"0";
}
else
{
  yourLbl.text = [currentEmplyCellDit objectForKey:@"total"]
}
于 2016-07-02T09:29:16.647 回答