I am accessing data from the json web services into my application. whenever there is no json data the app gets crashed, I am using nsjsonserialization is there anyway I can find out the empty array at response itself and display the error, thus not making app gets crashed.
Asked
Active
Viewed 675 times
-1
-
1Can you post how you doing the parsing of the JSON. – logixologist Nov 29 '13 at 06:00
-
Check once your response data. If response data have some length then only apply NSJson serialisation. – Bhumeshwer katre Nov 29 '13 at 06:02
4 Answers
1
try this . . . .
NSArray * dataArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if (dataArray != nil) {
// perform parsing
}

Shaik Riyaz
- 11,204
- 7
- 53
- 70
1
If you want to check if it is empty:
if ([myMutableArray count] != 0) { ... }
If you want to check if the variable is nil:
if (myMutableArray) { ... }
or:
if (myMutableArray != nil) { ... }

Pradhyuman sinh
- 3,936
- 1
- 23
- 38
0
The Exception as expected.. Try the condition whether it has the JSon Value Or Not
:
Something like this :
UserDetails = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
NSLog(@"User Det %@", UserDetails);
if (!UserDetails){
NSLog(@"Doesn't exist");
}else{
NSlog(@"%@",[UserDetails objectForKey:@"lastName"]);
.....
}

Kumar KL
- 15,315
- 9
- 38
- 60
0
Check if data is nil or if any error occurred or data length
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
NSLog(@"responseCode: %d", responseCode); //check response code
if ([data length] > 0 && error == nil)
//data downloaded -> continue
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
else if ([data length] == 0 && error == nil)
//empty reply
else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
// time out
else if (error != nil)
//error
}];

Suhit Patil
- 11,748
- 3
- 50
- 60