2014-09-04 32 views
0

這是我的JSON響應當我測試Web服務REST的CONSOLE如何解析iOS中7 JSON響應從.NET Web服務收到

{ 
"d": "{\"token\":\"45b95300-e1ff-4bc1-b45c-9b87babc4df3\",\"timestamp\":\"09/04/2014 12:40:12 PM\"}" 
} 

這是我的日誌詳細信息:

Server Response :<NSHTTPURLResponse: 0x8ea8b70> { URL: http://dev-jrn.stits.co:777/Authonticate.aspx/authenticate } { status code: 500, headers { 
"Content-Length" = 91; 
"Content-Type" = "application/json; charset=utf-8"; 
Date = "Thu, 04 Sep 2014 07:18:30 GMT"; 
Server = "Microsoft-IIS/7.5"; 
"X-Powered-By" = "ASP.NET"; 
jsonerror = true; 
} } 

我的客戶端代碼

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
     NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.textUserName text],[self.textPassword text]]; 
     NSLog(@"PostData: %@",post); 

     NSURL *url=[NSURL URLWithString:@"http://dev-jrn.stits.co:777/Authonticate.aspx/authenticate"]; 
     NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
            @"username", @"abc", 
            @"password", @"abc", 
            nil]; 

     NSError *error; 
     NSData *postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error]; 

     NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
     [request setValue:@"application/json; character=utf-8" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:postData]; 

     //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 
     [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error){ 


     // NSLog(@"Response code: %ld", (long)[response statusCode]); 
      if(error || !data){ 
       NSLog(@"Server Error : %@", error); 
      } 
     else 
     { 
      NSLog(@"Server Response :%@",response); 
      NSError* error; 
      NSDictionary* json = [NSJSONSerialization 
            JSONObjectWithData:data 
            options:kNilOptions 
            error:&error]; 

      NSArray* latest = [json objectForKey:@"d"]; 
      NSLog(@"items: %@", latest);//Returning items: (null) 
     } 
     } 
     ]; 

代碼工作正常,但我知道我在想念了一些東西。請幫助。 預先感謝

回答

0
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/json; character=utf-8" forHTTPHeaderField:@"Content-Type"]; 

替換的本

[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
+0

{的URL:http:// dev的-jrn.stits.co:777/Authonticate.aspx/authentica te} {狀態碼:200,標頭{「Cache-Control」= private; 「Content-Length」= 409; 「Content-Type」=「text/html; charset = utf-8」; 日期=「星期四,2014年9月04日09:59:31 GMT」; Server =「Microsoft-IIS/7.5」; 「X-AspNet-Version」=「4.0.30319」; 「X-Powered-By」=「ASP.NET」; }} 2014-09-04 18:03:41.355 LoginDemoUsingWebService [22086:3a03] items:(null) 替換後我仍然沒有得到要求可以建議如何解析此響應json數組或我如何獲取 – Asmi237 2014-09-04 10:08:33

+0

在你的NSLog(@「items:%@」,latest)=> items:(null)value.Put break point,並檢查你是否得到任何空字符串values.Can你檢查它。 – 2014-09-04 10:23:30

+0

我已檢查您的代碼。在獲取空值的用戶名和密碼中,您將如何獲得響應。 – 2014-09-05 06:14:36

0

代替該

NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
           @"username", @"abc", 
           @"password", @"abc", 
           nil]; 

嘗試

NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
           @"abc", @"username", 
           @"dch", @"password", 
           nil];