2014-09-11 2 views
-1

Webservice를 응답에서 수신내가 WebService에에서이 응답있으세요

{"d":"{"token":"b502645e-837f-4237-a6ff-d4323f2799dd","timestamp":"09/11/20147:46:43PM"}"} 

내가 같은 출력을 얻을 수 있도록이 문자열을 구문 분석 할 : 토큰 = b502645e-837f-4237-을 a6ff-d4323f2799dd timestamp = 09/11/20147 : 46 : 43PM 그래서 데이터베이스에 저장할 수 있습니다. Outer Dict (null)

이 사람이 나를 도울 수 :

내 코드

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
// The request is complete and data has been received 
// You can parse the stuff in your instance variable now 
/* 
NSError *errorJson=nil; 
NSString* OuterDict = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&errorJson]; 
NSLog(@"Outer Dictionary %@",OuterDict); 
*/ 
NSString *responseData = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding]; 

responseData = [responseData stringByReplacingOccurrencesOfString:@" " withString:@""]; 
responseData = [responseData stringByReplacingOccurrencesOfString:@"\\" withString:@""]; 
//NSString* encodedString = [responseData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
//NSLog(@"%@",encodedString); 
NSLog(@"Reponse data %@",responseData); 
NSError *errorJson=nil; 

NSData *jsonData = [responseData dataUsingEncoding:NSUTF8StringEncoding]; 
jsonData = [jsonData subdataWithRange:NSMakeRange(0, [jsonData length] - 1)]; 
NSDictionary* OuterDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&errorJson]; 
NSLog(@"Outer Dict %@",OuterDict); 

}

내가 출력으로 null을 얻고있다. 미리 감사드립니다.

+0

귀하의 JSON 이상한 것 ... 그것은 정확하지 보인다 않습니다. 나에게는 잘못된 두 개의'''(하나는'{'앞에 있고 다른 하나는''') 뒤에있다. – Larme

+0

[NSJSONSerialization으로 이스케이프 처리 된 JSON 문자열을 역 직렬화 할 수 있습니까?] (http://stackoverflow.com/questions/16948427/how-can-you-deserialize-an-escaped-json-string-with-nsjsonserialization) – memmons

+0

감사합니다. Larme 귀하의 코멘트 도움을 alot .. 사실 나를 구 했어요. – Asmi237

답변

1

메소드 상단에서 다음 스 니펫을 사용하십시오. JSON 데이터를 구문 분석하고 사전을 반환합니다. 필요에 따라 Dictionary에서 모든 작업을 수행 할 수 있습니다.

NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingAllowFragments error:&err]; 
NSLog(@"Response : %@", dictResponse); 

해피 코딩 :)