2013-06-12 54 views
0

我正在爲iPhone編寫本機應用程序,我對這個主題很陌生。
使用AFNetworking我請求(與POST)並處理JSON回覆。
但我注意到,一旦JSON響應更爲複雜:AFNetworking和compex JSON結構

{ 
    "isFound": "YES", 
    "timestamp": "2013-06-12 22:46:47", 
    "screenTitle": "Perla Review", 
    "placeName": "Perla", 
    "placeUniqueId": "101", 
    "placeCategory": "PUB", 
    "username": "@jim", 
    "userImgURL": "", 
    "gender": "male", 
    "infoMsg": "TBD", 
    "youLike": "Like", 
    "likesInfoMsg": "", 
    "revInfoList": [ 
     { 
      "type": 0, 
      "data": "", 
      "text": "" 
     }, 
     { 
      "type": 1, 
      "data": "2", 
      "text": "" 
     }, 
     { 
      "type": 2, 
      "data": "3", 
      "text": "" 
     } 
    ], 
    "commentsList": [] 
} 

然後AFNetworking無法讀取和構建子陣列(revInfoList & commentsList)。
我做錯了什麼,或AFNetworking不支持這種json結構?

這是我的目標C代碼請求數據和處理回覆:

static NSString *const myAPIBaseURL = @"http://somedomain.com/api/"; 

NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:myAPIBaseURL]]; 
NSString *reqPath = @"review/review_fullinfo"; 

// prepare params 
NSString *reqData_loginUsername = [[[Storage_Modal alloc] init] getLoginUsername]; 
NSString *reqData_currentCoordinatesLat = [[NSNumber numberWithDouble:[appDelegateInst.myCurrentLocation coordinate].latitude] stringValue]; 
NSString *reqData_currentCoordinatesLon = [[NSNumber numberWithDouble:[appDelegateInst.myCurrentLocation coordinate].longitude] stringValue]; 

NSString *reqData_reviewUniqueId = reviewUniqueId; 

NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys: 
          @"json", @"format", 
          reqData_loginUsername, @"loginUsername", 
          reqData_currentCoordinatesLat, @"currentCoordinatesLatitude", 
          reqData_currentCoordinatesLon, @"currentCoordinatesLongitude", 
          reqData_reviewUniqueId, @"reviewUniqueId", 
          nil]; 

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; 
[client registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
[client setDefaultHeader:@"Accept" value:@"application/json"]; 

[client postPath:reqPath parameters:parameters 
    success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     NSDictionary *xxx = responseObject; 
     // 'xxx' contains all the top level keys and values, but the sub arrays are empty... why? 

    } 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     NSLog(@"SERVER ERROR: %@", error); 
    } 

**注:請原諒我,如果有在Objective-C代碼語法錯誤,我不得不從多個功能收集代碼。

謝謝。

+1

您應該向我們展示您用於訪問revInfoList和commentsList的代碼。從你已經顯示的這兩個列表是不清楚的,因爲你的評論暗示 –

+0

@TimDean我正在查看DEBUG中的'responseObject'。我看到'revInfoList'鍵和3個空數組。所以這個問題似乎比訪問密鑰的代碼更基礎。 – thedp

+1

你看到3個「空」數組的事實告訴我,JSON解析器在這裏正確地做了一些事情(因爲你在revInfolist下有3個孩子)。他們應該是NSDictionary對象,而不是NSArray對象,所以我想知道你在「DEBUG」中看到的是什麼(我認爲你的意思是你在XCode的調試器中查看它)。它是否顯示3個類型爲NSArray的對象恰好是空的?根據你在調試器中的看法,它可能根本不是3個空數組。 –

回答

1

AFNetworking使用NSJSONSerialization將您的JSON轉換爲基礎對象,通常是NSDictionary或NSArray。 NSJSONSerialization嚴格遵守RFC 4627

你張貼的JSON響應是有效的JSON,所以只有3個原因,我能想到的行爲你描述:

  1. 你的服務器返回一個不同的有效載荷比你張貼在這裏的一個
  2. 您的服務器編碼的有效載荷錯誤
  3. 你是不正確有關什麼xxx的NSDictionary包含

你可以看着測試#1 g你的AFJSONRequestOperationresponseStringresponseData屬性。 #2和#3都可以通過仔細檢查xxx對象進行測試。如果您在這裏發佈這些對象的輸出,我們可以幫助您更好地進行診斷。

您的AFNetworking代碼和您預期的JSON都看起來很好,所以我不認爲這是AFNetworking的問題。

+0

謝謝你的洞察力。一旦我開始瞭解我發現問題的步驟。正如你所說,一切都應該工作,而且確實如此。只有一次我打印了'responseString'的描述,我注意到XCode調試器是我浪費時間的來源。一切都在那裏,但調試器太麻煩顯示所有的信息。當我打印「xxx」的描述時,它就在那裏。我學到了一些新東西。非常感謝你:) – thedp

+0

這很奇怪。您可能想要發佈一個關於該問題的單獨問題。您可以在目標的Build Settings中查看您的「優化級別」設置 - 如果未設置爲「none」,編譯器可能會丟棄它所知道永遠不會被訪問的數據。我建議在調試過程中將此設置保留爲「無」。 –