2012-08-09 107 views
0

我現在正在學習Xcode,我有一個項目是使用php從Mysql數據庫中提取數據並通過json傳遞給我的應用程序。在數據庫中,所有變量都設置爲utf8_bin。Xcode json錯誤

這裏是PHP:

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

這裏是輸出的JSON的snipet:

[{"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 
} 

when我使用這個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模擬器中「重置內容和設置」。

有什麼想法?

回答

0

返回的對象似乎是一個數組,但你的代碼處理它像一個字典(JSON對象/散列)

這個錯誤告訴你:它說,該消息objectForKey:(這是NSDictionary的方法)被髮送到的__NSCFArray一個實例,這是一個實現類的NSArray,因此我假設......

+0

感謝您的回答,我正在做更多的研究並將結果發佈,但您的幫助已經指出了我的正確方向。 – chris 2012-08-09 07:02:43

0

是的,我有一個想法 -

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

數組沒有字典ionaries。他們不響應objectForKey 他們響應objectForIndex;

你以爲你有一個數組,當你有一本字典。

常見的JSON錯誤。

+0

感謝您的回答,我正在進行更多的研究並將結果發佈,但您的幫助已將我指向正確的方向。 – chris 2012-08-09 07:03:12

+0

NSDictionary * json = [NSJSONSerialization JSONObjectWithData:responseData // 1 options:kNilOptions error:&error]; NSArray * latestLoans = [json objectForKey:@「loans」]; // 2 從JSON方法返回的對象不是字典它的數組(列表) – 2012-08-09 07:07:33

+0

請參閱上面的答案 – 2012-08-09 07:15:48

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); 
    } 

.... -

Got it?