JSON - 将JSON数据提取到数据模型中

时间:2015-09-24 16:32:46

标签: ios objective-c json unrecognized-selector

我尝试将 JSON 数据存储到数据模型中以使其易于使用,但它只存储一个NSString *状态的数据“它不存储其他两个( “NSArray * content”和“NSString * serverTime”)。

这是我第二次使用 JSON ,所以我对此并不了解。

这是来自NSLog(@“item%@”,itemDic);

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

这是错误日志。

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

这是我的代码

- (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

这是一些JSON数据

 {
    "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
}

2 个答案:

答案 0 :(得分:1)

听起来你会从试用JSONModel

中受益

http://www.jsonmodel.com/

导入框架,修改模型以完全匹配JSON,JSONModel将为您完成剩下的工作

答案 1 :(得分:0)

我还向您推荐Jsonformatter https://jsonformatter.curiousconcept.com。该站点可以帮助您调试JSON数据。