2013-07-31 105 views
1

我正在使用AFNetworking從URL獲取JSON,JSON被解析並且應該轉換成NSData格式,然後將其轉換爲數組。但它似乎沒有工作。我想我是愚蠢的,因爲它以前的工作,但現在 -從AFNetworking轉換JSON

-(void)loadFromServer{ 

     NSString *trendsURL = [NSString stringWithFormat:@"http://myurl.com/data.json"]; 

     NSURL *url = [NSURL URLWithString:trendsURL]; 
     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

     AFJSONRequestOperation *operation = [AFJSONRequestOperation 
              JSONRequestOperationWithRequest:request 
              success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) { 
               self.results = [json valueForKeyPath:@"data"]; 
              } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
              }]; 

     [operation start]; 




     NSError *anError = nil; 
     NSArray *parsedElements = [NSJSONSerialization JSONObjectWithData:elementsData 
                    options:NSJSONReadingAllowFragments 
                    error:&anError]; 

     for (NSDictionary *aModuleDict in parsedElements){ 
      MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict]; 
      [elements addObject:aMosModule]; 
     } 
    } 

的數據顯示像視圖分別在集合中,沒有與此不同的觀點沒有問題,當我訪問文件數據的工作原理而不是服務器。

-(void)loadFromDisk{ 
    NSString *pathString = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"]; 
    NSData *elementsData = [NSData dataWithContentsOfFile:pathString]; 

    NSError *anError = nil; 
    NSArray *parsedElements = [NSJSONSerialization JSONObjectWithData:elementsData 
                   options:NSJSONReadingAllowFragments 
                   error:&anError]; 

    for (NSDictionary *aModuleDict in parsedElements){ 
     MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict]; 
     [elements addObject:aMosModule]; 
    } 
} 

的JSON是牽強的,像這樣:

{ 
    "imageLink": "http://link.com/image.jpg", 
    "size": 1, 
    "title": "Food" 
    } 

我給它另一個去,和它仍然不工作。結果是一個聲明的NSArray。

-(void)loadFromServer{ 

    NSString *trendsURL = [NSString stringWithFormat:@"http://url.com/data.json"]; 

    NSURL *url = [NSURL URLWithString:trendsURL]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation 
             JSONRequestOperationWithRequest:request 
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) { 
              NSLog(@"Custom Mosaic: %@", json); 
              self.results = [json valueForKeyPath:@"data"]; 
             } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
             }]; 

    [operation start]; 



    for (NSDictionary *aModuleDict in self.results){ 
     MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict]; 
     [elements addObject:aMosModule]; 
    } 
} 

我覺得這個問題實際上可能是這在繞過一點點出場,出於某種原因,它不會拿起在AFNetworking操作分析數據的結果。

-(id)init{ 
    self = [super init]; 

    if (self){ 
     elements = [[NSMutableArray alloc] init]; 
     [self loadFromServer]; 
    } 

    return self; 
} 

// WWDC 2012 proposed method 
+ (CustomMosDatasource *)sharedInstance { 
    static CustomMosDatasource *sharedInstance; 
    if (sharedInstance == nil) 
     sharedInstance = [CustomMosDatasource new]; 
    return sharedInstance; 
} 

#pragma mark - MosViewDatasourceProtocol 

-(NSArray *)mosElements{ 
    NSArray *retVal = elements; 
    return retVal; 
} 

日誌的結果僅僅是JSON:

Custom Mosaic: { 
    data =  (
       { 
      imageFilename = "http://distilleryimage6.instagram.com/9969123a6af311e2b6c722000a9d0edd_7.jpg"; 
      size = 1; 
      title = "Title"; 
     }, 
+3

1)什麼不起作用?錯誤訊息?預期的和實際的結果? - 2)'elementsData'從哪裏來?它沒有在您的代碼中設置或初始化。 - 3)AFJSONRequestOperation異步工作**! –

+0

對不起,我已經添加了一些細節,發生的問題是集合視圖中沒有顯示任何內容。這是某種形式的解析錯誤,但我無法弄清楚什麼。 –

+0

你的實際代碼是什麼?在我看來,你錯過了請求的異步方法。嘗試在成功回調下執行''for''語句。 – amb

回答

0
AFJSONRequestOperation *operation = [AFJSONRequestOperation 
             JSONRequestOperationWithRequest:request 
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) { 
              self.results = [json valueForKeyPath:@"data"]; 
              NSError *anError = nil; 
              NSArray *parsedElements = [NSJSONSerialization JSONObjectWithData:elementsData 
                   options:NSJSONReadingAllowFragments 
                   error:&anError]; 
    for (NSDictionary *aModuleDict in parsedElements){ 
     MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict]; 
     [elements addObject:aMosModule]; 
    } 
             } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
             }]; 

    [operation start]; 
+0

感謝您的答案,但這似乎並不奏效,我已經給了它另一個嘗試,如上所述。但它真的不想工作。 –

0

您的JSON似乎不具備的keyPath 「數據」,去掉valueForKeyPath:@ 「數據」,請貼在你的問題中「NSLog(@」Custom Mosaic:%@「,json)」的結果「。 該操作是異步的。請求返回數據後調用成功塊。請把代碼放在這樣的成功塊中

-(void)loadFromServer{ 

    NSString *trendsURL = [NSString stringWithFormat:@"http://url.com/data.json"]; 

    NSURL *url = [NSURL URLWithString:trendsURL]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation 
             JSONRequestOperationWithRequest:request 
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) { 
              NSLog(@"Custom Mosaic: %@", json); 
              // your json doesn't have keyPath "data" 
              self.results = json; 
              // Codes moved into success block 
              for (NSDictionary *aModuleDict in self.results){ 
               MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict]; 
               [elements addObject:aMosModule]; 
              } 

             } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
             }]; 

    [operation start]; 
} 
+0

感謝您的代碼,但這似乎也無法正常工作,JSON按照AFNetorking正確提取並解析,它是for(NSDictionary ...部分似乎是問題 –

+0

NSLog的結果是什麼( @「Custom Mosaic:%@」,json) – situee

+0

我已將它添加到問題本身。 –