2013-07-06 47 views
3

JSON對象是這樣的。如何顯示在標籤的字符串值,如果值是零和若干

 { 
     "id": "1", 
     "subTotal": "20000.00", 
     "total": "124565.00", 
     "timeOfTrans": "3555525", 
     "status": "1", 
     "name": "gjgjgf", 
     "email": "[email protected]", 
     "level": "gjfgj", 
     "teamId": "1" 
    }, 
    { 
     "id": "2", 
     "subTotal": null, 
     "total": null, 
     "timeOfTrans": null, 
     "status": null, 
     "name": "Rajendra", 
     "email": "[email protected]", 
     "level": "gfjgfjg", 
     "teamId": "1" 
    } 

我想在標籤中顯示JSON字符串 「總」。當「總數」爲空時,我想顯示'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]; 

驗證碼正確顯示,但我顯示空的標籤時,「總」爲空。

我想顯示0時, 「總」 是NULL

如何解決這個... 感謝。

+0

檢查我更新的答案哥們。 – Rajneesh071

回答

2

可以使病情檢查字符串中的空,然後用任何你想要替換此。

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

我在表格視圖中顯示標籤。 –

+1

當「total」值爲NULL時,它顯示。當「total」值爲NULL時,我想顯示0。 –

+0

只是把這個代碼..它將運行,它在我結束做工精細.. – Rajneesh071

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

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

功能這肯定會幫助你。必須嘗試.....

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

歡迎的StackOverflow!如果你想編輯你的答案並添加一些解釋爲什麼這段代碼能夠回答這個問題,那將是一件好事。只是粘貼一段代碼是沒有幫助的。 – trincot

相關問題