是否有任何良好的教程/源代碼來解析jSon數據到cocos2d項目?我知道如何解析jSon(也是XML)到xcode中並顯示到tableview中,但我需要爲我的cocos2d項目執行此操作。 這是我要怎樣做:cocos2d json解析iphone應用程序
#import "Eighties.h"
#import "HelloWorldLayer.h"
#import "GameScene.h"
#import "JSON.h"
#define kLatestKivaLoansURL @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"
@implementation Eighties
@synthesize responseData;
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
Eighties *layer = [Eighties node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if((self=[super init])) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *bg = [CCSprite spriteWithFile:@"bg.jpg"];
[bg setPosition:ccp(winSize.width/2, winSize.height/2)];
[self addChild:bg z:0];
/*
CCMenuItem *menuItems = [CCMenuItemImage itemWithNormalImage:@"back_pink.png" selectedImage:@"back_blue.png" block:^(id sender) {
NSLog(@"Pressed");
[[SimpleAudioEngine sharedEngine] playEffect:@"tongue-clap.wav"];
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[GameScene scene] withColor:ccWHITE]];
}];
*/
CCMenuItem *menuItems2 = [CCMenuItemImage itemWithNormalImage:@"back_pink.png" selectedImage:@"back_blue.png" target:self selector:@selector(loadData)];
menuItems2.position = ccp(winSize.width/2-50, winSize.height/2-50);
CCMenu *menu = [CCMenu menuWithItems:menuItems2, nil];
menu.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:menu];
}
return self;
}
-(void)test {
NSLog(@"Success");
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Test Method Called"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
-(void)loadData
{
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kLatestKivaLoansURL]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
self.responseData = nil;
}
#pragma mark -
#pragma mark Process loan data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"loans"];
[responseString release];
//choose a random loan
for (int i=0; i<=18; i++) {
NSDictionary* loan = [latestLoans objectAtIndex:i];
//fetch the data
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
//float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
//NSString* name = [loan objectForKey:@"name"];
//NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];
//set the text to the label
/*
label.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another $%.2f, please help",
name,country,outstandingAmount
];
*/
NSLog(@"%d",i);
//NSLog(@"%@",label.text);
NSLog(@"\n");
/*
UIAlertView *message = [[UIAlertView alloc] initWithTitle:name
message:country
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
*/
}
}
@end
抱歉,我不能格式化我這裏貼的代碼......它可能是難以閱讀。我的問題是,我如何解析並顯示來自遠程URL的jSon數據到「Cocos2d」項目中。我有一個類是cocos2d中的「CCLayer」的子類,而不是UIViewController的子類。我知道解析jSon for UIViewController的子類,但我無法爲CCLayer的子類做到這一點。你知道關於這個的任何教程嗎? – jeewan
爲什麼你不能爲CCLayer的子類做同樣的事情?更多信息將有所幫助:) –
我修復了這些問題。問題是:我使用的JSON解析器中有一些錯誤。我嘗試從這裏的最新版本:https://github.com/stig/json-framework/downloads,它工作正常。我發佈的代碼只是包含最新的JSON .h文件。 – jeewan