2013-12-23 63 views
0

我正在使用AFNetworking 2從服務器獲取一些json數據。這是員工數據,我創建了一個員工對象和一個員工NSMutableArray。使用AFNetworking解析JSON

我的服務正在運行。如果我吐出響應對象的日誌,我會得到600名員工。我正在努力的是如何讓這些人進入我的員工NSMutableArray。

responseObject有一個id類型,意思是沒有類的對象,對嗎?我嘗試將ID更改爲員工,因爲這是我想要返回的,但這並不起作用。然後我注意到responseObject實際上是所有的對象,所以我嘗試了員工,但沒有奏效。

所以我想我可以通過對象循環,但因爲它不是一個真正的NSMutableArray,我不明白如何。

任何幫助將不勝感激。

布萊恩

- (IBAction)jsonTapped:(id)sender 
{ 

    NSMutableArray *employees; 
    employee *thisEmployee = [employee new]; 

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"XXXXXX" password:@"XXXXXX"]; 

    AFHTTPRequestOperation *operation = [manager GET: @"https:/XXXXXXXXXXX/something" 
              parameters: [self jsonDict] 
              success:^(

                 AFHTTPRequestOperation *operation, id responseObject) 
    { 

                 NSLog(@"Submit response data: %@", responseObject);} 
              failure:^(AFHTTPRequestOperation *operation, NSError *error){ 
               NSLog(@"Error: %@", error);} 
                ]; 
    [operation start]; 
} 

這是我成功集團內部:

NSMutableArray *employees = (NSMutableArray *)responseObject; 

    NSLog(@"Count of array to begin: %lu", (unsigned long)[employees count]); 
    NSLog(@"JSON RESULT %@", responseObject); 

    FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; 
        [db open]; 
    for (employee *thisemployee in employees) { 
    BOOL success = [db executeUpdate:@"INSERT INTO employees (firstName,fullName,email) VALUES (?,?,?,?);",thisemployee.firstName,thisemployee.lastName,thisemployee.fullName,thisemployee.emailAddress, nil]; 
    if (success) {} // Only to remove success error 
    NSLog(@"DDD %@", thisemployee); 
+1

響應對象將是NSArray或NSDictionary,具體取決於指定的JSON。您需要將其轉換爲正確的類型才能訪問數據。 –

回答

1

你只需要獲得responseObject的success塊內,並將其轉換爲數組,像這樣:

NSArray *employees = (NSArray *)responseObject; 

AFNetworking已經爲你解析了Json。根據你的響應類型(可能是一個數組),你需要做的只是施加到NSArrayNSDictionary

+1

我認爲這是有道理的,並嘗試過,但有些地方是錯誤的。我把responseObject作爲一個僱員數組(就像你上面那樣),但是這些值都是垃圾。我遍歷一個循環,嘗試逐個加載我的員工,這樣我就可以寫出一個文件[我修改了上面的代碼],但每次都崩潰。 –

2

嘗試您的操作管理器的響應串設置爲[AFJSONResponseSerializer串行]

你有你的requestSerializer集,而不是你responseSerializer。 (:

myOperationManager.responseSerializer = [AFJSONResponseSerializer serializer]; 
0

這裏的技巧是使用AFJSONRequestOperation自動地轉換爲NSDictionaryNSArray取決於返回的JSON類型接收到的響應我用下面的代碼爲返回一個JSON編碼的響應簡單的GET請求:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:SERVER_PATH]]; 
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" 
                 path:@"/rank.json" 
                parameters:@{@"score" : intToString(highestScore)}]; 
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 
{ 
    rankingReceived([[((NSDictionary *)JSON) objectForKey:@"rank"] intValue]); 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) 
{ 
    rankingReceived(-1); 
}]; 
[operation start]; 

請求格式(GET):http://server/rank.json?score=x

響應(JSON)"{rank:y}"

希望這有助於!

UPDATE: 好像AFJSONRequestOperation已被棄用 - 我想看看Replacement for AFJSONRequestOperation in AFNetworking 2.x一種替代。

+0

用戶表示他們正在使用AFNetworking 2. AFJSONRequestOperation不再可用。 – lordB8r

+0

AFJSONRequestOperation在最新版本的AFNetworking中已被棄用。所以我們可以用什麼來代替AFJSONRequestOperation @zorayr – Dalvik

+0

http://stackoverflow.com/questions/21294178/replacement-for-afjsonrequestoperation-in-afnetworking-2x – Zorayr