我正在使用JSONModel开发一个ios应用程序,但我的RootTabeViewController
没有显示我使用过JSONModel的任何数据。你可以在github上看到我的项目:
https://github.com/ShahriatHossain/myprojectios
,这是我的JSON数据格式:
[
{
"businessID": 1,
"name": "Favorite BC"
},
{
"businessID": 2,
"name": "Related to me"
},
{
"businessID": 3,
"name": "Reported by me"
},
{
"businessID": 4,
"name": "Ready for approval"
},
{
"businessID": 5,
"name": "Approved BC"
}
这是我的BusinessModel.h代码
#import "JSONModel.h"
@protocol BusinessModel @end
@interface BusinessModel : JSONModel
@property (assign, nonatomic) int businessID;
@property (strong, nonatomic) NSString* name;
@end
,这是我的BusinessManager.h代码
#import "JSONModel.h"
#import "BusinessModel.h"
@interface BusinessManager : JSONModel
@property (strong, nonatomic) NSArray<BusinessModel>* businesses;
@end
,这是我的RootTableViewController.m代码
#import "RootTableViewController.h"
#import "SecondTableViewController.h"
#import "JSONModelLib.h"
#import "BusinessManager.h"
#import "HUD.h"
@interface RootTableViewController (){
BusinessManager* _businessFeeds;
}
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
//businesses = [NSArray arrayWithObjects:@"Favorite BC",@"Related to me",@"Reported by me", @"Ready for approval",@"Approved BC", nil];
//show loader view
[HUD showUIBlockingIndicatorWithText:@"Fetching JSON"];
//fetch the feed
_businessFeeds = [[BusinessManager alloc] initFromURLWithString:@"http://localhost:8888/json/business.json"
completion:^(JSONModel *model, JSONModelError *err) {
//hide the loader view
HUD hideUIBlockingIndicator];
//json fetched
NSLog(@"loans: %@", _businessFeeds.businesses);
[self.tableView reloadData];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_businessFeeds.businesses count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BusinessModel* business = _businessFeeds.businesses[indexPath.row];
static NSString *simpleTableIdentifier = @"BusinessCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
// Configure the cell...
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", business.name];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if([segue.identifier isEqualToString:@"arrayDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BusinessModel* business = _businessFeeds.businesses[indexPath.row];
SecondTableViewController *destViewController = segue.destinationViewController;
destViewController.businessName = [NSString stringWithFormat:@"%@", business.name];
destViewController.title = destViewController.businessName;
}
}
@end
我在代码中做错了什么我不知道。我是ios app开发的新手。
有人能帮助我吗?
答案 0 :(得分:0)
我在这里做错了是错误的json格式,这里是正确的:
{&#34;商家&#34;:[ { &#34; businessID&#34;:1, &#34;姓名&#34;:&#34;最喜欢的BC&#34; }, { &#34; businessID&#34;:2, &#34;姓名&#34;:&#34;与我相关&#34; }, { &#34; businessID&#34;:3, &#34;姓名&#34;:&#34;由我报告&#34; }, { &#34; businessID&#34;:4, &#34;名称&#34;:&#34;准备好批准&#34; }, { &#34; businessID&#34;:5, &#34;姓名&#34;:&#34;批准BC&#34; } ]}
我的应用正在加载json数据。