2012-05-06 2 views
0

메신저 내 응답을 구문 분석하는 SBJSON 사용하여 버스 어떻게 든 난 내 데이터를 얻을 수 없습니다. 어떻게 응답해야합니까?내 목표 -c 프로그램에서 json 응답을 구문 분석

{"StatusCode":0,"Message":"email already exists","Content":{"HasApplication":false,"IsFaceBook":false,"UserActive":false,"UserAuthenticationType":0,"UserCredits":0,"UserDayAdded":0,"UserEmail":null,"UserEmailWasVerified":false,"UserGuid":null,"UserID":0,"UserSellerApproved":false,"UserTokef":0},"TotalCount":0} 

내가 이렇게 시작 :

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [responseData setLength:0]; 
    NSLog(@"%@",response); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
    NSLog(@"Data recived");  
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription], 
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
    responseData = nil; 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"conenction loading finished"); 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    SBJsonParser *parser = [[SBJsonParser alloc] init]; 
    NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString error:nil]; 
} 

하지만 무슨 다음 내가이 상태 코드 값과 사용자 ID가 필요합니다.

+0

인쇄 jsonDictionary하고 스스로를 참조하십시오, 당신은 어떻게 사전에 키 값/객체를 검색 할 것인가? – 0x8badf00d

답변

0

하나는 당신이 사전을 가지고, 당신은 예를 들어, 사용할 수 있습니다 :

json으로 문자열의 모습에서
NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString error:nil]; 
NSNumber * statusCode = [jsonDictionary objectForKey:@"StatusCode"]; 
NSString * message = [jsonDictionary objectForKey:@"Message"]; 
NSDictionary * content = [jsonDictionary objectForKey:@"Content"]; 
// etc... 
0

, 세 키 값 쌍, 다른 사전에 인 그 중 하나가있는 사전을 가지고 여러 개의 키 값 쌍.

수 있어야 뭔가 같은 : 당신이 당신의 NSMutableDictionary에 JSON responseString을 할당하면 그래서 처리하는 완전히 다른 노트에

NSNumber *statusCode = [jsonDictionary objectForKey:@"StatusCode"]; 
NSDictionary *contentDict = [jsonDictionary [email protected]"Content"]; 
NSString *userID = [contentDict [email protected]"UserID"]; 

, 당신이가는 경우는 웹 - 많은 일을해야합니다 서비스 상호 작용을 통해 AFNetworking을 심각하게 고려하고 싶을 수 있습니다. 그것은 당신의 삶을 바꿀 것입니다 :)

또한 왜 NSMutableDictionary가 jsonDictionary가 필요한지 알 수 없습니다. 나중에 NSDictionary를 사용하지 않으면 오버 헤드가 적습니다.

0

잘 ..... NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString NSMutableDictionary가 아닌 NSDictionary 일 수 있습니다. 다음 :

NSString *statusCode = [jsonDictionary valueForKey:@"StatusCode"]; 
NSDictionary *contentDict = [jsonDictionary [email protected]"Content"]; 
NSString *userID = [contentDict [email protected]"UserID"];