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」をラベルに表示したい。「合計」がnullの場合、「合計」が数値​​の場合に「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];

このコードは正しく表示されますが、「合計」がnullの場合、ラベルにnullが表示されます。

「total」がNULLの場合は0を表示したい

これを解決する方法...ありがとう。

4

4 に答える 4

2

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

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 に答える
0
if([currentEmplyCellDit objectForKey:@"total"] == [NSNull null]) 
{
  yourLbl.text=@"0";
}
else
{
  yourLbl.text = [currentEmplyCellDit objectForKey:@"total"]
}
于 2016-07-02T09:29:16.647 に答える