我尝试像本教程那样存储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;
它将返回null。
删除该行后,它将起作用。
那么你可以查看我的代码,看看哪里出错了?
这是我尝试获取的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
这是FOTTFeed.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
答案 0 :(得分:1)
JSON Detail是一个数组:
"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中,&#34;内容&#34;我认为,财产应该是NSArray。
答案 1 :(得分:0)
BWJSONMatcher是一个轻量级库,可帮助您轻松地将JSON字符串或JSON对象与数据模型匹配。如果数据模型的所有属性与json数据中的键一致, BWJSONMatcher 将自动匹配它们。
您可以找到详细的示例here。