2014-07-20 50 views
1

我是新來的ios和它的發展。我有乾淨的代碼設置正確AFNetworking。我的基地URl的json Encording有JSON對象和數組以及值。在我的JSON輸出中,我希望獲得「縮略圖」的值,每次我都會得到Null。請幫助我獲取我的json輸出的「名稱,縮略圖,ID,圖像」。請找到我的NSDictionary類型的打印對象的NSlog。AFNetworking GET json值並保存NSMutablearray

2014-07-20 09:08:33.110 WADTourisum[1157:60b] Reachability Flag Status: -R ------- networkStatusForFlags 
2014-07-20 09:08:33.879 WADTourisum[1157:60b] JSON: { 
    Main =  (
       { 
      id = 1; 
      "image_bundle_id" = 1; 
      images =    (
       "http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/1.jpg", 
       "http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/2.jpg", 
       "http://wearedesigners.net/clients/clients12/tourism/images/guides/oceans/slide_images/3.jpg" 
      ); 
      name = OCEAN; 
      thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/ocean.jpg"; 
     }, 
       { 
      id = 2; 
      "image_bundle_id" = 23; 
      images =    (
       "http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/1.jpg", 
       "http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/2.jpg", 
       "http://wearedesigners.net/clients/clients12/tourism/images/guides/heritages/slide_images/3.png" 
      ); 
      name = Heritage; 
      thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/heritage.jpg"; 
     }, 
       { 
      id = 3; 
      "image_bundle_id" = 0; 
      images =    (
      ); 
      name = "Tea Country"; 
      thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/teaCountry.jpg"; 
     }, 
       { 
      id = 4; 
      "image_bundle_id" = 0; 
      images =    (
      ); 
      name = "WILD LIFE"; 
      thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/wildLife.jpg"; 
     }, 
       { 
      id = 5; 
      "image_bundle_id" = 0; 
      images =    (
      ); 
      name = Culture; 
      thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/culture.jpg"; 
     }, 
       { 
      id = 6; 
      "image_bundle_id" = 0; 
      images =    (
      ); 
      name = "NIGHT LIFE"; 
      thumbnail = "http://wearedesigners.net/clients/clients12/tourism/images/guides/thumbs/nightLife.jpg"; 
     } 
    ); 

}

我的代碼片段

-(void) retriveData 
{ 



    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

    [manager GET:@"http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php" 
    parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     self.posts =(NSDictionary *)responseObject; 
     self.post =self.posts[@"thumbnail"]; 




     NSLog(@"JSON: %@", self.post); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please log into internetet" 
                  message:[error localizedDescription] 
                  delegate:nil 
                cancelButtonTitle:@"Ok" 
                otherButtonTitles:nil]; 
     [alertView show]; 
    }]; 







} 
+0

您是否還可以包含以下結果的代碼:'每次我獲得空值時都會獲取「縮略圖」的值# – dezinezync

+0

@dezinezync 2014-07-20 18:56:08.062 WADTourisum [1551 :60b]可達性標誌狀態:-R ------- networkStatusForFlags 2014-07-20 18:56:08.848 WADTourisum [1551:60b] JSON:(null) –

+0

我猜prop'post'是一個'NSArray'? – dezinezync

回答

4

你所得到的迴應,我看到的唯一的問題是你是不是能夠正確地檢索值。

從JSON中獲取數據時,請記住以何種格式獲取數據,即要麼獲取數組或字典。

看到你的迴應你正在獲取數組本身包含字典。

使用下面的代碼來獲取值

NSArray *array = [responseObject valueForKey:@"Main"]; 

    for (NSDictionary *dict in array) { 
     NSInteger ids = [[dict valueForKey:@"id"] integerValue]; 
     NSString *name = [dict valueForKey:@"name"]; 
     NSString *thumbnail = [dict valueForKey:@"thumbnail"]; 
     NSArray *arrImages = [dict valueForKey:@"images"]; 
     //You can use them accordingly 

    } 

希望這有助於你。快樂編碼:)

+0

邑它對我的作品非常感謝你可以請告訴我特定值的索引請我的意思是= 3有這樣的縮略圖那樣:) –

+0

@RehanK你能詳細說一下 – Shubhendu

+0

我想做一個有thubanali和名字的兩個屬性。並且我想用索引(java調用對象數組)來保持這個類的引用我該如何做到這一點謝謝你的提前到目前爲止:) –