0

我现在正在学习 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 模拟器中“重置内容和设置”。

有任何想法吗?

4

3 回答 3

0

The returned object appears to be an array but your code is treating it like a dictionary (json object/hash)

The error tells you this: it say that the message objectForKey: (which is a method on NSDictionary) is being sent to an instance of __NSCFArray, which is an implementation class of NSArray, hence my supposition...

于 2012-08-09T05:10:19.770 回答
0

Yes I have an Idea -

-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x6a10400

Arrays are not dictionaries. They do not respond to objectForKey They respond to objectForIndex;

You are thinking you have an array when you have a dictionary.

Common JSON mistake.

于 2012-08-09T05:11:00.203 回答
0

这是您的数据:

它是一个列表

从这里开始 --> "[" 然后对象从这里开始 "{"

[{"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 - liveDea 开头的项目

JSON 说一个列表是一个数组,一个对象是一个字典,所以翻转你的代码

 (void)fetchedData:(NSData *)responseData {
  //parse out the json data

     NSError* error;
     NSArray* latestLoans = [NSJSONSerialization 
                  JSONObjectWithData:responseData //1

                  options:kNilOptions 
                  error:&error];

     NSLog(@"loans: %@", latestLoans); //3

     for (int i=0; i < latestLoans.count; i++) 
     {
        NSDictionary *myLoan = (NSDictionary*)[latestLoans objectAtIndex:i];
        NSLog(@"loan:%@", myLoan);
     }

……

知道了?

于 2012-08-09T07:14:15.743 回答