我现在正在学习 Xcode,我有一个项目使用 php 从 Mysql 数据库中提取数据并通过 json 将其传递给我的应用程序。在数据库中,所有 varchars 都设置为 utf8_bin。
这是php:
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
echo json_encode($this->Idea_model->get($id));
这是输出 JSON 的片段:
[{"id":"1","title":"JWT blood sucka","objective":"test ","mission":"test","design_time":"80","development_time":"80","votes":"0","user_id":"0","date_created":"2012-08-03","date_modified":"2012-08-03","active":"1"},{"id":"2","title":"ford - liveDealer","objective":"to increce ","mission":"thid id a es","design_time":"80","development_time":"80","votes":"1","user_id":"1","date_created":"0000-00-00","date_modified":"0000-00-00","active":"1"}]
在 xcode 中,我使用这个函数来提取 JSON [参考教程:http://www.raywenderlich.com/5492/working-with-json-in-ios-5]
(void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* latestLoans = [json objectForKey:@"loans"]; //2
NSLog(@"loans: %@", latestLoans); //3
}
当我使用教程中的这个 JSON 文件时,它可以工作 http://api.kivaws.org/v1/loans/search.json?status=fundraising
但是当我使用我的 JSON 文件时,我收到以下错误。
[8690:207] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x6a10400
Current language: auto; currently objective-c
显然,我的 JSON 输出存在问题,因为我在 PHP 文件中打印了教程文件中的内容,并且效果也很好。
我也尝试过在 iOS 模拟器中“重置内容和设置”。
有任何想法吗?