0

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.

Amir Foghel
  • 853
  • 1
  • 12
  • 28

3 Answers3

0

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...
MByD
  • 135,866
  • 28
  • 264
  • 277
0

From the looks of the JSON string, you have a Dictionary with three Key Value pairs, one of those being another Dictionary with several Key Value pairs. So to deal with that once you have assigned the JSON responseString to your NSMutableDictionary:

Should be something like:

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

On a completely different note, if you are going to be doing a lot of web-service interaction, you may want to take a serious look at AFNetworking. It will change your life :)

Also, I can't see why you would need jsonDictionary to be an NSMutableDictionary. Unless you are changing it later on, use NSDictionary, it has less overhead.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
0

well..... NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString Maybe it's not NSMutableDictionary, but NSDictionary. and then:

NSString *statusCode = [jsonDictionary valueForKey:@"StatusCode"];
NSDictionary *contentDict = [jsonDictionary objectForKey@"Content"];
NSString *userID = [contentDict valueForKey@"UserID"];
anna
  • 662
  • 5
  • 28