2013-10-07 12 views
0

我想使用nsdictionary獲取這些數據。我的數據是這樣的:使用NSDictionary執行使用JSON數據的GET

{ 
    "Id": 1, 
    "Items": [ 
    { 
     "Id": 1, 
     "CorrectAnswer": "sample string 3", 
     "Difficulty": 4, 
     "CategoryId": 5, 
     "CategoryName": "sample string 6", 
     "AccountId": 7 
    }, 
    { 
     "Id": 1, 
     "PicUrl": "sample string 2", 
     "CorrectAnswer": "sample string 3", 
     "Difficulty": 4, 
     "CategoryId": 5, 
     "CategoryName": "sample string 6", 
     "AccountId": 7 
    } 
    "StartTime": "2013-10-07T00:24:22.0048045+00:00" 
} 

這是嘗試:

- (IBAction)play:(id)sender { 
    [self performSegueWithIdentifier:@"playSegue" sender:self]; 

    NSString *GameId; 
    NSNumber *ID; 
    NSURL *PictureURL; 
    NSString *ans1; 
    NSString *ans2; 
    NSString *ans3; 
    NSString *ans4; 
    NSArray *answers = [[NSArray alloc]initWithObjects:ans1,ans2,ans3,ans4, nil]; 
    NSString *correctAnswer; 
    NSString *difficulty; 
    NSString *categoryID; 
    NSString *categoryName; 
    NSNumber *accountID; 
    NSArray *items = [[NSArray alloc]initWithObjects:ID,PictureURL,answers,correctAnswer,categoryID,categoryName,accountID, nil]; 

    NSDictionary *result = [self requestWithPath:[NSString stringWithFormat:@"someurlhere"] Method:@"GET" 
              Data:[NSDictionary dictionaryWithObjectsAndKeys: 
                GameId,@"GameId", 
                items, @"Items", 
                nil] 
              Parse:YES]; 
    NSLog(@"the result is %@",result); 


} 


-(NSDictionary *)requestWithPath:(NSString *)path 
           Method:(NSString *)requestMethod 
           Data:(NSDictionary *)requestData 
           Parse:(BOOL)parse 
    { 
     if(path)  NSLog(@"%@", path); 
     if(requestData) NSLog(@"%@", requestData); 

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%@", path] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 
     [request setHTTPMethod:requestMethod.uppercaseString]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

     if(requestData && [NSJSONSerialization isValidJSONObject:requestData]) 
     { 
      NSError *error = nil; 
      NSData *data = [NSJSONSerialization dataWithJSONObject:requestData options:0 error:&error]; 

      [request setHTTPBody:data]; 
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
      [request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"]; 
     } 

     NSURLResponse *response = nil; 
     NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL]; 

     if(responseData) 
      NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]); 

     if(parse && responseData) 
     { 
      NSError *serializationError = nil; 
      return [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&serializationError]; 
     } 
     else if (responseData) 
      return @{@"string": [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]}; 

     return nil; 
    } 

控制檯導致

2013-10-06 19:01:16.477 FlickRest[41610:a0b] someurlhere 
2013-10-06 19:01:16.478 FlickRest[41610:a0b] { 
} 
2013-10-06 19:01:17.186 FlickRest[41610:a0b] the result is (null) 

我的問題

除了沒有得到我想要的結果之外,我相信我沒有以正確的方式做到這一點。有人能指出我的錯誤,或者建議一種方式來如何正確地轉換JSON數據?謝謝。

+0

你能否澄清一下你的問題。你到底在問什麼?你發佈的數據是一本字典,就這樣使用它。 –

+0

@PeterFoti,我編輯問題。感謝您花時間看看它。 – Matt

回答

0

試試看this 並嘗試使用調試。

PS。你的代碼是不正確的。

+0

感謝您的回覆。你是在談論我嘗試使用路徑方法存儲數據或請求的方式嗎?你能更具體嗎? – Matt

+0

都:)但首先你需要創建正確的網絡請求。 – Bimawa

+0

我不確定您是否閱讀了代碼,但這是我使用requestWithPath方法執行的操作,並將其作爲[self requestWithPath ...進行調用...我請求的方式對於簡單數據工作正常。我遇到了需要做複雜對象映射的部分(標識符,URL,可能的答案等等),這就是爲什麼我首先提出這個問題的原因。 – Matt