2014-01-06 13 views
0

我是新來的iOS讀取本地JSON文件,請幫助我,我怎麼能使用的Objective-C如何使用Objective-C的

SBJSON *parser = [[SBJSON alloc] init]; 

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"auth_request" ofType:@"json"]; 


NSError *error = nil; 

NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error]; 
NSString *json_string = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding]; 



NSArray *statuses = [parser objectWithString:json_string error:nil]; 



for (NSDictionary *status in statuses) { 


    NSLog(@"%@ - %@", [status objectForKey:@"loginId"], [status objectForKey:@"secret"]); 
} 
+0

首先要學習,也不要緊,你正在使用'xcode'你可以用文本編輯器來這樣做的,它仍然是相同的。 – Popeye

+2

'NSArray * statuses = [NSJSONSerialization JSONObjectFromData:JSONData options:0 error:&error];'(其中'error'是通常的NSError對象)。 –

回答

-1

只需添加 的NSArray * tempArray = [json_string讀本地JSON文件JSONVALUE] 代替 NSArray * statuses = [parser objectWithString:json_string error:nil];

不要忘了導入

+0

哪些文件需要導入 – user3156748

+0

請勿在第三方JSON庫上啓動新手。除非有充分的理由否則他應該使用Apple的NSJSONSerialization。 –

+0

可以請你告訴我如何解析json文件的代碼或發送給我一些好的鏈接 – user3156748