所以我对 ios 开发非常陌生,我正在创建我的第一个应用程序,它连接到服务器并提取一些信息。我遵循了许多不同的教程,并设法从不同的地方撤回了一些 json。我遇到的问题是我需要提取的信息没有根元素,我需要更改什么才能将信息发送到我的应用程序?
以下是我正在使用的代码:
#import "leagueTableViewController.h"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kjsonURL [NSURL URLWithString: @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]
@interface leagueTableViewController ()
@end
@implementation leagueTableViewController{
NSMutableArray *jsonResults;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
kjsonURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
jsonResults = [json objectForKey:@"loans"];
[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 the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return [jsonResults count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *appsdict = [jsonResults objectAtIndex:indexPath.row];
NSString *VersionString = [appsdict objectForKey:@"funded_amount"];
NSString *priceString = [appsdict objectForKey:@"loan_amount"];
cell.textLabel.text = [appsdict objectForKey:@"name"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Version: %@ Price: $ %@ USD",VersionString,priceString];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSDictionary *appsdict = [jsonResults objectAtIndex:indexPath.row];
//
// NSString *appURL = [appsdict objectForKey:@"trackViewUrl"];
//
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];
}
@end
这是上面代码使用的 json 示例,您可以看到根元素:
{
"paging": {
"page": 1,
"total": 873,
"page_size": 20,
"pages": 44
},
"loans": [{
"id": 544449,
"name": "Sajad",
"description": {
"languages": ["en"]
},
"status": "fundraising",
"funded_amount": 0,
"basket_amount": 0,
"image": {
"id": 1325179,
"template_id": 1
},
"activity": "Cloth & Dressmaking Supplies",
"sector": "Retail",
"use": "to renovate his shop and increase his textile inventory",
"location": {
"country_code": "IQ",
"country": "Iraq",
"geo": {
"level": "country",
"pairs": "33 44",
"type": "point"
}
},
"partner_id": 166,
"posted_date": "2013-04-04T15:40:01Z",
"planned_expiration_date": "2013-05-04T15:40:01Z",
"loan_amount": 2400,
"borrower_count": 1
}
这是我需要获取的 json,但它没有根元素,我需要做什么才能在我的应用程序中显示它:
[{
"position": 1,
"team_id": 10260,
"team": "Manchester United",
"teamshort": "Man Utd",
"played": 30,
"won": 25,
"drawn": 2,
"lost": 3,
"for": 70,
"against": 31,
"difference": 39,
"home": {
"played": 15,
"won": 14,
"drawn": 0,
"lost": 1,
"for": 39,
"against": 15,
"difference": 24
},
"away": {
"played": 15,
"won": 11,
"drawn": 2,
"lost": 2,
"for": 31,
"against": 16,
"difference": 15
},
"points": 77,
"info": "championsleague"
}