Search code examples
iosobjective-cjsonunrecognized-selector

JSON - fetch JSON Data into data model


I try to store JSON data into data model to make it easy to use but it store only one data which are NSString *status" it doesn't store the other two ("NSArray * content" and "NSString * serverTime").

This is the second time that i use JSON so I don't know much about it.

This is from the NSLog(@"item %@",itemDic);

2015-09-24 23:11:40.697 fott[628:98405] item status

This is the error log.

2015-09-24 23:11:40.698 fott[628:98405] -[NSTaggedPointerString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xa007375746174736

Here is my code

- (NSArray*)getData{
NSMutableArray *result = [[NSMutableArray alloc] init];

NSString *path =@"http://87.251.89.41/application/11424/article/get_articles_list";
if(path){
    NSURL *json = [NSURL URLWithString:path]; // jsonString

    NSData *jsonData = [NSData dataWithContentsOfURL:json];
      encoding:NSUTF8StringEncoding];

    if(jsonData){
        NSError *error = nil;
        NSArray *jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
         NSLog(@"jsonObjects %@",jsonObjects);

        for(NSDictionary *itemDic in jsonObjects){
            PlaceData *item = [[PlaceData alloc] init];
            NSLog(@"item %@",itemDic);
            for(NSString *key in itemDic){

                if([item respondsToSelector:NSSelectorFromString(key)]){

                    if([key isEqualToString:@"content"]){

                        NSArray *contentJsonObjects = [itemDic objectForKey:key];
                        NSMutableArray *contents = [[NSMutableArray alloc] init];

                        for(NSDictionary *contentDic in contentJsonObjects){
                            Content *content = [[Content alloc] init];

                            for(NSString *contentKey in contentDic){
                                if([contentKey isEqualToString:@"content"]){

                                    NSArray * contentDetailJsonObjects = [contentDic objectForKey:contentKey];
                                    NSMutableArray * contentDetails = [[NSMutableArray alloc] init];

                                    for(NSDictionary *contentDetailDic in contentDetailJsonObjects){
                                        Contents * contentDetail = [[Contents alloc]init];

                                       for (NSString * contentDetailKey in contentDetailDic){
                                           [contentDetail setValue:[contentDetailDic valueForKey:contentDetailKey] forKey:contentDetailKey];
                                        }
                                        [contentDetails addObject:contentDetail];
                                    }
                                    [content setValue:contentDetails forKey:contentKey];
                                }


                                [content setValue:[contentDic valueForKey:contentKey] forKey:contentKey];
                            }



                            [contents addObject:content];
                        }
                        // Set value to content
                        [item setValue: contents forKey:key];
                    }else{
                        // For general detail
                        [item setValue:[itemDic valueForKey:key] forKey:key];

                    }
                }
            }

            [result addObject:item];
        }
    }else{
        NSLog(@"JSON NOT FOUND");
    }
}else{
    NSLog(@"PATH NOT FOUND");
}

return result;
}

@end

This are some JSON Data

 {
    "status": "success",
    "content": [
      {
        "id": 11447,
        "title": "Arsenal's Afobe targets a full season on loan",
        "dateTime": "30.10.2014 06:38",
        "tags": [],
        "content": [
            {
                "type": "text",
                "subject": "Benik Afobe",
                "description": "The 21-year-old was last capped by the Three Lions in February 2013 when he featured for the Under-21s against Sweden at Walsall in a 4-0 win.\r\n\r\nA series of injuries since then have restricted his progress both on the club and international front and he spent the second half of last season on loan at Sheffield Wednesday."
            }
        ],
        "ingress": "Arsenal and England Under-21s striker Afobe is hoping a full season on loan can help to rejuvenate his career.\r\n",
        "image": "http://87.251.89.41/sites/default/files/afobe-celeb-dier.jpg",
        "created": 1407476424,
        "changed": 1414664497
    },
    {
        "id": 11445,
        "title": "Alex Pritchard",
        "dateTime": "03.12.2014 06:36",
        "tags": [
            "11457"
        ],
        "content": [
            {
                "type": "text",
                "subject": "Alex Pritchard",
                "description": "After impressing at the County Ground, he has agreed a similar spell at Griffin Park as well as extending his contract with Spurs until 2016.\r\n\r\n\"Alex is a player I have watched for many years,\" revealed Brentford boss Mark Warburton to BrentfordFC.co.uk.\r\n\r\n\"I have followed his career at Tottenham from a young age. He is very good tactically and technically with excellent dead-ball delivery."
            }
        ],
        "ingress": "Spurs starlet Alex Pritchard signs on loan for Brentford",
        "image": "http://87.251.89.41/sites/default/files/alex-pritchard.jpg",
        "created": 1407476299,
        "changed": 1417695236
    }
],
"serverTime": 1443015978
}

Solution

  • Sounds like you'd benefit from trying out JSONModel

    http://www.jsonmodel.com/

    Import the framework, modify your models to match the JSON exactly, JSONModel will do the rest for you