iProgrammer
iProgrammer

Reputation: 3107

How to check null from json string

When a JSON Response has no data, it will return Response: [ ].

Here is my code

NSURL *url = [NSURL URLWithString:urlString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];


    [request startSynchronous];
    NSError *error = [request error];

    NSString *response =nil;
    if (!error) {
        response = [request responseString];

        NSLog(@"Response: %@",response);
}

How should I check whether the response has a value or not?

I tried if([response isEqualToString:@""]|| [response length]==0) but it is not working. I also tried if(response == nil);.

Upvotes: 3

Views: 484

Answers (3)

Ravi Chokshi
Ravi Chokshi

Reputation: 1166

Try ,

if ([response isKindOfClass:[NSNull class]])

This one worked for me ..

Upvotes: 0

Jose Luis
Jose Luis

Reputation: 3361

Try with

if([response isEqualToString:@"[]"])

Upvotes: 5

Marsson
Marsson

Reputation: 685

Well... If the answer when JSON has no answer is [] you could try:

if([response isEqualToString:@"[]"]);

Upvotes: 3

Related Questions