I have to parse this JSON on iOS.
{
"log_by_dates": {
"logs": [
{
"date": "Wednesday 5 December 2012",
"exercises": "0",
"workouts": "0",
"log_entries": "0"
},
{
"date": "Tuesday 4 December 2012",
"exercises": "4",
"workouts": "2",
"log_entries": "7"
}
]
}
}
I have written following code to parse it;
NSArray *logs = [[(NSDictionary*)results objectForKey:@"log_by_dates"] objectForKey:@"logs"];
for (NSDictionary *aLog in logs) {
Log *newLog = [[Log alloc] initWithDate:[aLog objectForKey:@"date"] withExercises:[aLog objectForKey:@"exercises"]
withWorkouts:[aLog objectForKey:@"workouts"]];
if (!data) {
data = [[NSMutableArray alloc] init];
}
But problem is that, sometimes I get the JSON value like this;
{
"log_by_dates": {
"logs":
{
"date": "Wednesday 5 December 2012",
"exercises": "0",
"workouts": "0",
"log_entries": "0"
}
}
}
Which makes my code to crash.
Please guide me, which if() else condition I use to check that whether the incoming JSON object contains multiple records of the single one before parsing so that I code write appropriate code to handle the dictionary or array.
Thanks,
Please update your code like this.