Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
[
  {
    "AT" : null,
    "EA" : null,
    "AD2" : "",
    "OP" : null,
    "AdsLst" : [

    ],
    "EMs" : null,
    "ND" : {
      "MN" : null,
      "FN" : "Sony",
      "LN" : "J"
    },
    "ZP" : "23456",
    "CT" : 1,
    "PP" : 0,
    "cId" : 161464,
    "Pos" : null,
    "WPExt" : null,
    "OS" : 0,
    "HP" : "(345) 678-7890",
    "BTTC" : 0,
    "PWD" : null,
    "UId" : 627,
    "DId" : 0,
    "UN" : null,
    "CY" : "Salt lake",
    "Comp" : null,
    "PMC" : 0,
    "AEa" : null,
    "AnD" : null,
    "DOB" : "1\/05\/1979",
    "IDel" : false,
    "ST" : "UH",
    "AD" : "2345 Symonds street",
    "Mob" : null,
    "WP" : "(234) 324-6576",
    "PL" : 0,
    "Potcus" : false
  },
  {
    "AT" : null,
    "EA" : null,
    "AD2" : "",
    "OP" : null,
    "AdsLst" : [

    ],
    "EMs" : null,
    "ND" : {
      "MN" : null,
      "FN" : "Augustine",
      "LN" : "Gomes"
    },
    "ZP" : "43567",
    "CT" : 1,
    "PP" : 0,
    "cId" : 161466,
    "Pos" : null,
    "WPExt" : null,
    "OS" : 0,
    "HP" : "(456) 457-6588",
    "BTTC" : 0,
    "PWD" : null,
    "UId" : 627,
    "DId" : 0,
    "UN" : null,
    "CY" : "test",
    "Comp" : null,
    "PMC" : 0,
    "AEa" : null,
    "AnD" : null,
    "DOB" : null,
    "IDel" : false,
    "ST" : "AZ",
    "AD" : "2345 test",
    "Mob" : null,
    "WP" : "(678) 987-0890",
    "PL" : 0,
    "Potcus" : false
  },
  {
    "AT" : null,
    "EA" : null,
    "AD2" : "",
    "OP" : null,
    "AdsLst" : [

    ],
    "EMs" : null,
    "ND" : {
      "MN" : null,
      "FN" : "Jenny",
      "LN" : "X"
    },
    "ZP" : "45678",
    "CT" : 1,
    "PP" : 0,
    "cId" : 161467,
    "Pos" : null,
    "WPExt" : null,
    "OS" : 0,
    "HP" : "(435) 644-6545",
    "BTTC" : 0,
    "PWD" : null,
    "UId" : 627,
    "DId" : 0,
    "UN" : null,
    "CY" : "Sallas",
    "Comp" : null,
    "PMC" : 0,
    "AEa" : null,
    "AnD" : null,
    "DOB" : null,
    "IDel" : false,
    "ST" : "TX",
    "AD" : "2433 Main street",
    "Mob" : null,
    "WP" : "(657) 656-8798",
    "PL" : 0,
    "Potcus" : false
  },
  {
    "AT" : null,
    "EA" : null,
    "AD2" : "",
    "OP" : null,
    "AdsLst" : [

    ],
    "EMs" : null,
    "ND" : {
      "MN" : null,
      "FN" : "BLVD Cont F1",
      "LN" : "Blvd Contact L1"
    },
    "ZP" : "",
    "CT" : 1,
    "PP" : 0,
    "cId" : 161473,
    "Pos" : null,
    "WPExt" : null,
    "OS" : 0,
    "HP" : null,
    "BTTC" : 0,
    "PWD" : null,
    "UId" : 627,
    "DId" : 0,
    "UN" : null,
    "CY" : "",
    "Comp" : null,
    "PMC" : 0,
    "AEa" : null,
    "AnD" : null,
    "DOB" : null,
    "IDel" : false,
    "ST" : "",
    "AD" : "",
    "Mob" : null,
    "WP" : null,
    "PL" : 0,
    "Potcus" : false
  },

In this response just i want to display "FN","LN","CT","PP","cId","Pos","AT","EA","AD2" these tag value.... then which is null value just want to display "Apple"....

Can anyone help me

my code is

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSError *error;
    json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"json.... %@",json);


    id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];

    NSLog(@"jsonObject=%@", jsonObject);

    NSDictionary *checkArray=[jsonObject valueForKey:@"ND"];


    cheDisk=[checkArray valueForKey:@"FN"];
    cheDisk1=[checkArray valueForKey:@"LN"];

    NSLog(@"FN =%@",cheDisk);
    NSLog(@"LN =%@",cheDisk1);

}
share|improve this question
 
Err, how about if (object == nil) text = @"Apple";? –  trojanfoe May 21 '13 at 10:55
 
I'm not sure I understand what you're trying to do? –  dreamlax May 21 '13 at 10:56
 
yes...i want to display "FN","LN","CT","PP","cId","Pos","AT","EA","AD2" these tag value.... then if null means just display "Apple" string –  Ssn May 21 '13 at 10:58
 
@sivakumar see my answer its working for me –  Rushabh May 21 '13 at 10:58
 
show 4 more comments

5 Answers

null is how the NSNull singleton logs. So:

if (cheDisk == [NSNull null]) {
    // cheDisk is null
}
share|improve this answer
add comment
up vote 2 down vote accepted
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{


    NSError *error;
    json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"json.... %@",json);


    id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];

    NSLog(@"jsonObject=%@", jsonObject);

    NSDictionary *checkArray=[json valueForKey:@"ND"];


    NSArray *tel = [checkArray valueForKey:@"FN"];



    for (id photo in tel)
    {
        if (photo == [NSNull null])
        {
            // photo is null

        }
        else
        {
            // photo isn't null. It's an array
            NSArray *innerPhotos = photo;
            NSLog(@"photo... %@",innerPhotos);
        }

    }



    cheDisk1=[checkArray valueForKey:@"LN"];

    NSLog(@"FN =%@",tel);


    [ContactTableview reloadData];


}
share|improve this answer
add comment

try with

   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


  NSString *strOP = [results objectForKeyOrNil:@"OP"];

 - (id)objectForKeyOrNil:(id)key {
     id val = [self objectForKey:key];
     if ([val isEqual:[NSNull null]] || val==nil) {
         return nil;
     }

     return val;
 }
share|improve this answer
add comment

Hope this helps.

cheDisk=[checkArray valueForKey:@"FN"];
        cheDisk1=[checkArray valueForKey:@"LN"];.

    if([cheDisk isEqualToString:@"null"]){
        cheDisk = @"Apple";
    }
    if([cheDisk1 isEqualToString:@"null"]){
        cheDisk1 = @"Apple";
    }


        NSLog(@"FN =%@",cheDisk);
        NSLog(@"LN =%@",cheDisk1);
share|improve this answer
add comment

you can use :

if (cheDisk isKindOfClass:[NSNull class]) {
    // cheDisk is null
}

Hope this will help you.

All the best !!!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.