Search code examples
iphoneobjective-cjsonsbjson

parsing json response in my objective-c program


im using SBJSON to parse my response, bus somehow i cant get my data out. how should i parse this reponed ?

{"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}

i start like this :

- (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];
}

but whats next ?i need the StatusCode value and the UserID.


Solution

  • One you have the dictionary, you can use it, for example:

    NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString error:nil];
    NSNumber * statusCode = [jsonDictionary objectForKey:@"StatusCode"];
    NSString * message = [jsonDictionary objectForKey:@"Message"];
    NSDictionary * content = [jsonDictionary objectForKey:@"Content"];
    // etc...