Faiz Fareed Faiz Fareed - 2 months ago 5x
iOS Question

Getting error while converting Json data to Object in iOS

I am using an API to convert Json Data into Object data,
please visit the API I am using
Please Visit the API page which I am using

Here is the snap of my code with highlighted issues

and here is the raw code

-(void) retrieveData{
NSURL * url = [NSURLURLWithString:getDataUrl];

NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil];

NSLog(@"JsonArray %@", jsonArray);

//setup yougaArray
yougaArray = [[NSMutableArray alloc] init];

//Loop through our jsonArray

for (int i = 0; i<jsonArray.count; i++)
NSString * yId = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"id"];
// NSString * yId = [[jsonArray objectAtIndex:i]objectForKey:@"id"];
NSString * yName = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"name"];
NSString * yDescription = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"description"];
NSString * yImage = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"image"];

//Add the city object to our citiesArray

[yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];

[self.tableView reloadData];



This is the JSON:

    "meta": {
        "status": "200",
        "msg": "OK"
    "data": {
        "total_pages": 0,
        "total_categories": 2,
        "current_page": 1,
        "next_page": 0,
        "categories": [{
            "id": "2",
            "name": "Articles",
            "description": "Yoga Articles",
            "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"
        }, {
            "id": "1",
            "name": "Poses",
            "description": "Yoga Poses",
            "image": "http:\/\/yoga.lifehealthinfo.com\/uploads\/images\/50_50\/86289272image86289272.jpg"

Your JSON is a NSDictionary at top level! Not a NSArray! Also, avoid doing all objectForKey:/objectAtIndex: in the same line/instruction, it's harder to read, but also harder to debug, especially when you don't know what you are doing. Also, when there is an error parameter, use it, don't put nil.


NSError *errorJSON = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errorJSON];
if (errorJSON)
    NSLog(@"ErrorJSON: %@", errorJSON);
NSDictionary *dataJSON = [jsonDict objectForKey:@"data"];
NSArray *allCategoriesJSON = [dataJSON objectForKey:@"categories"];
for (NSUIInteger i = 0; i < allCategoriesJSON.count; i ++)
    NSDictionary *aCategoryJSON = [allCategoriesJSON objectAtIndex:i];
    NSString yID = [aCategoryJSON objectForKey:@"id"];
    NSString yName = [aCategoryJSON objectForKey:@"name"];
    NSString yDescription = [aCategoryJSON objectForKey:@"description"];
    NSString yImage = [aCategoryJSON objectForKey:@"image"];
    [yougaArray addObject:[[Youga alloc] initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]];