0

I'm learning Xcode at the moment and i have a project that is pulling data from a Mysql database using php and passing it to my app via json. In the database all varchars are set to utf8_bin.

here is the php:

header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
echo json_encode($this->Idea_model->get($id));

here is a snipet of the outputted 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"}]

in xcode I'm using this function to pull in the JSON [reference tutorial: 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
}

when i use this JSON file from the tutorial it works http://api.kivaws.org/v1/loans/search.json?status=fundraising

but when i use my JSON file i get the following error.

[8690:207] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x6a10400
Current language:  auto; currently objective-c

obviously there is an issue with my JSON output as i printed the contents from the tutorial file in my PHP file and that worked as well.

i also have tried "reset contents and settings" in the iOS simulator.

any ideas?

4

3 に答える 3

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 に答える