-1

我尝试像本教程一样存储 JSON

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

在本教程中,它在 LoanModel.h 中声明 LocationModel。

@property (strong, nonatomic) LocationModel* location;

哪个在 projectDemo 中效果很好但是当我尝试在我的项目中使用它时

@property(strong,nonatomic) ContentDetailModel *content;

它将返回空值。

删除该行后它将起作用。

所以你能检查我的代码,看看哪里错了吗?

这是我尝试获取的 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
    }
  ]
}

这是我在 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

这是我在 ContentDetailModel.h 中的模型

@interface ContentDetailModel : JSONModel

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

@end

这是 FOTFeed.h

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

@interface FOTTFeed : JSONModel

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

@end

这是我的代码

#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
4

2 回答 2

1

JSON 详细信息是一个数组:

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

在您的模型 ContentListModel 中,我认为“内容”属性应该是 NSArray。

于 2015-09-25T18:59:11.637 回答
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 .

于 2015-11-10T08:56:58.010 回答