2014-09-26 37 views
-1

在ios7此代碼的工作,但現在我得到的錯誤: - [__ NSCFString objectForKeyedSubscript:]:無法識別的選擇發送到實例獲取代碼「無法識別的選擇發送到實例」爲iOS8上

.m文件

//get the JSON response 
NSDictionary *jsonData = [NSJSONSerialization 
          JSONObjectWithData:urlData 
          options:NSJSONReadingMutableContainers 
          error:&error]; 
//Parses the "success" value 
success = [jsonData[@"success"] integerValue]; 

//Was it successful? 
if(success){ 
    //successful, save the profile gathered into global gMyProfile 
    NSArray *profileJSON=jsonData[@"myProfile"]; 
    for (NSDictionary* dict in profileJSON) 
    { 
     NSLog(@"First_Name: %@", dict [@"first_name"]); 
    ... 

錯誤發生在NSLog聲明上,並且來自一點研究,它抱怨字典[@「first_name」]);

login.py

.... 
#Query for user 
db_cursor = db.cursor(MySQLdb.cursors.DictCursor) 
db_query = """SELECT users.email, users.first_name, users.profile_pic_path, \ 
       FROM users,data WHERE users.email='%s' \ 
       AND users.user_id=data.user_id""" % user_email 
db_cursor.execute(db_query) 

#If there is one record containing the username check password 
if(db_cursor.rowcount == 1): 
    user_profile = db_cursor.fetchone() 
... 
    json_obj= {'success': 1, 'myProfile': user_profile,} 
... 

JSON輸出:

{'myProfile': {'first_name': 'Matt', 'email': '[email protected]', 'profile_pic_path': 'default'}, 'success': 1} 

所有這些代碼是工作,我沒有改變任何東西。 任何幫助將不勝感激!!!!

+0

沒有看到JSON,我們不能做太多。 – 2014-09-26 17:18:16

+0

用JSON輸出編輯帖子:) – lr100 2014-09-26 17:26:44

+0

轉到json.org並研究JSON語法。學習只需要5-10分鐘。然後回到這裏並解釋你的代碼是如何構成的。 – 2014-09-26 17:37:57

回答

0

dict不是一本字典。它是一個字符串。打印它,你會知道它是哪個字符串。

更可能的是,您的JSON不是您認爲的那樣。特別是,myProfile值是字符串而不是字典。

如果fetchone()返回None,會發生什麼情況;你最終在你的JSON中的字符串?

+0

我只是困惑,爲什麼這在ios7中運行良好。它完美地解析和保存數據。不適宜研究它:) – lr100 2014-09-26 17:56:38

+1

@ lr100 - 當有多個數據集時,另一端可能發送一個數組,而只有一個數據集時發送一個對象。這並不常見,但並非前所未聞。如果是這種情況,則需要測試收到的內容以確定「myProfile」是數組還是字典。 – 2014-09-26 19:19:55

+0

@HotLicks說什麼;你應該拆開接收到的JSON並驗證數據的結構。即使在生產中,您也可能想要針對JSON數據中的意外類型加強代碼。 – bbum 2014-09-26 20:22:54

0

改變了的.m

//Was it successful? 
if(success){ 
    //successful, save the profile gathered into global gMyProfile 
    NSArray *profileJSON=jsonData[@"myProfile"]; 
    for (NSDictionary* dict in profileJSON) 
    { 
    ... 

//Was it successful? 
if(success){ 
    //successful, save the profile gathered into global gMyProfile 
    NSDictionary* dict=jsonData[@"myProfile"]; 
    //no more for loop 
    ... 

蛇皮JSON模塊返回單個字典而不是單個字典的陣列時,有一種元素。如果有多個,它將​​返回一個字典數組。

不知道爲什麼這並沒有給我在ios7的麻煩,但在ios8。

相關問題