-1

I try to store JSON like this tutorial

http://www.touch-code-magazine.com/how-to-fetch-and-parse-json-by-using-data-models/

In the tutorial it declare LocationModel in LoanModel.h.

@property (strong, nonatomic) LocationModel* location;

Which work great in the projectDemo but when I try to use it in my project like this

@property(strong,nonatomic) ContentDetailModel *content;

It will return null.

It will work after I delete that line.

So can you guy check my code to see where is it wrong?

Here is JSON data that i try to fetch

{

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

Here is my model in ContentListModel.h

@protocol ContentListModel

@end

@interface ContentListModel : JSONModel

@property (assign,nonatomic) NSString * id;
@property (strong,nonatomic) NSString * title;
@property (strong,nonatomic) NSString * dateTime;
@property (strong,nonatomic) NSArray  * tags;
@property (strong,nonatomic) NSString * ingress;
@property (strong,nonatomic) NSString * image;
@property (strong,nonatomic) NSString * created;
@property (strong,nonatomic) NSString *changed;

@property(strong,nonatomic) ContentDetailModel *content;

@end

Here is my model in ContentDetailModel.h

@interface ContentDetailModel : JSONModel

@property(strong,nonatomic) NSString * type;
@property(strong,nonatomic) NSString * subject;
@property(strong,nonatomic)NSString * description;

@end

Here is FOTTFeed.h

#import <Foundation/Foundation.h>
#import "JSONModel.h"
#import "ContentListModel.h"

@interface FOTTFeed : JSONModel

@property (strong , nonatomic) NSArray<ContentListModel> *content;

@end

Here is my code

#import "FeedList.h"
#import "SWRevealViewController.h"
#import "UIImageView+WebCache.h"
#import "JSONModelLib.h"
#import "FOTTFeed.h"
#import "HUD.h"
#import "FeedDetail.h"

@interface FeedList (){
    FOTTFeed *feed;
}

@end

@implementation FeedList{
    NSArray *tableData;

}
@synthesize tableView;
@synthesize detailView;

- (void)viewDidLoad {
    [super viewDidLoad];



    _menu.target = self.revealViewController;
    _menu.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    //background image
    UIImage * background = [UIImage imageNamed:@"background_2.jpg"];
    UIImageView * imageView = [[UIImageView alloc]initWithImage:background];
    [self.tableView setBackgroundView:imageView];




    //set navigation bar colour
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:(29/255.0) green:(43/255.0) blue:(58/255.0) alpha:1.0]];

}


-(void)viewDidAppear:(BOOL)animated{

    [HUD showUIBlockingIndicatorWithText:@"Loading..."];

    //fetch the feed
    feed = [[FOTTFeed alloc]initFromURLWithString:@"http://87.251.89.41/application/11424/article/get_articles_list" completion:^(JSONModel * model, JSONModelError *err){



        //hide the loader view
        [HUD hideUIBlockingIndicator];

        //json fetch
       NSLog(@"Content:%@",feed.content);

        [self.tableView reloadData];

    }];

}

-(void)viewWillAppear:(BOOL)animated{



    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [feed.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.tag = indexPath.row;
    [cell setOpaque:NO];
    [cell setBackgroundColor: [UIColor clearColor]];


    ContentListModel *data = [feed.content objectAtIndex:indexPath.row];

    UIImageView *thumbnailImageView = (UIImageView *)[cell viewWithTag:100];
    UILabel *titleNews = (UILabel *)[cell viewWithTag:200];
    UILabel *Time = (UILabel *)[cell viewWithTag:300];
    UITextView * ingress = (UITextView *)[cell viewWithTag:400];

    ingress.editable = false;

    titleNews.text = data.title;


    ingress.text = data.ingress;


    Time.text = data.dateTime;



    [thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", data.image]] placeholderImage:[UIImage imageNamed:@"dot.png"]];


    return cell;

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"showFeedDetail"]){
        NSIndexPath *indexpath = [self.tableView indexPathForSelectedRow];
        FeedDetail *detailView = segue.destinationViewController;
        detailView.data = [feed.content objectAtIndex:indexpath.row];


    }
}

@end

2 Answers2

1

The JSON Detail is an array:

"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."
        }
    ]

In your model ContentListModel, the "content" property, I think, shoud be a NSArray.

0

BWJSONMatcher is a lightweight library which helps you easily match a JSON string or JSON object up with your data model. If all properties of your data models are consistent with the keys in json data, BWJSONMatcher will automatically match them up.

You can find the detailed examples here .

Burrows Wang
  • 249
  • 1
  • 7