Iphone 没有根元素的Json
所以我对ios开发非常陌生,我正在创建我的第一个应用程序,它连接到服务器并获取一些信息。我已经学习了许多不同的教程,并设法从不同的地方提取了一些json。我的问题是,我需要提取的信息没有根元素,我需要更改什么才能将信息发送到我的应用程序 下面是我正在使用的代码:Iphone 没有根元素的Json,iphone,ios,objective-c,json,Iphone,Ios,Objective C,Json,所以我对ios开发非常陌生,我正在创建我的第一个应用程序,它连接到服务器并获取一些信息。我已经学习了许多不同的教程,并设法从不同的地方提取了一些json。我的问题是,我需要提取的信息没有根元素,我需要更改什么才能将信息发送到我的应用程序 下面是我正在使用的代码: #import "leagueTableViewController.h" #define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAUL
#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"
}
如果在尝试读取前去掉第一个括号,第二个块将正确解析:
NSData *strippedData = [data subdataWithRange:NSMakeRange(1, data.length-1)];
就您收到的错误而言,这不是NSJSONSerialization错误,它是说返回的对象是NSArray类型(即使您期望的是NSDictionary类型),并且您试图对其调用objectForKey:
,这不是NSArray的方法。你应该分析数组,看看它是否包含你需要的信息。可能看起来像这样:
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
jsonResults = [[jsonArray objectAtIndex:0] objectForKey:@"loans"];
祝你好运。如果在阅读前去掉第一个括号,第二个块将正确解析:
NSData *strippedData = [data subdataWithRange:NSMakeRange(1, data.length-1)];
就您收到的错误而言,这不是NSJSONSerialization错误,它是说返回的对象是NSArray类型(即使您期望的是NSDictionary类型),并且您试图对其调用objectForKey:
,这不是NSArray的方法。你应该分析数组,看看它是否包含你需要的信息。可能看起来像这样:
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
jsonResults = [[jsonArray objectAtIndex:0] objectForKey:@"loans"];
祝你好运。第二个“没有根元素”的响应是无效的JSON。这就是为什么它不能被解析。不存在没有根元素的JSON——只有有效的JSON和损坏的、无效的JSON。您有两个选择:
使用JSON,您需要事先了解结构。向您发送数据的API会让您知道这一点 您收到错误的原因是您将NSArray视为NSDictionary。这里有一个例子 应将其解析为NSArray:
[{
username: 'johndoe'
}]
这将是一种误解:
{
username: 'johndoe'
}
您的数据与我介绍的第一个案例类似。因此,要获取数据,您需要:
NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
[[json objectAtIndex: 0] objectForKey:@"thekey"]
使用JSON,您需要事先了解结构。向您发送数据的API会让您知道这一点 您收到错误的原因是您将NSArray视为NSDictionary。这里有一个例子 应将其解析为NSArray:
[{
username: 'johndoe'
}]
这将是一种误解:
{
username: 'johndoe'
}
您的数据与我介绍的第一个案例类似。因此,要获取数据,您需要:
NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
[[json objectAtIndex: 0] objectForKey:@"thekey"]
那么,您正在检索的JSON在NSJSONSerialization调用中返回一个错误?这是两个完全不同的响应。。。顺便说一句……第一组json工作正常,但第二组返回错误\uu\NSCFArray objectForKey:这就是我在试图显示第二个响应时遇到的问题,但我发现的所有教程和示例都只适用于第一种响应。@paulpwr,因为第二个和第一个结构完全不同。。。当然,您必须以另一种方式管理第二个。因此,您正在检索的JSON在NSJSONSerialization调用中返回一个错误?这是两个完全不同的响应。。。顺便说一句……第一组json工作正常,但第二组返回错误\uu\NSCFArray objectForKey:这就是我在试图显示第二个响应时遇到的问题,但我发现的所有教程和示例都只适用于第一种响应。@paulpwr,因为第二个和第一个结构完全不同。。。当然,您必须以另一种方式管理第二个。