1

I am trying to learn how to parse JSON data so I can handle big databases. I wrote code to login into a website.

I have following JSON data from a successful login request:

JSON string : correct username and password [{"user_id":"7","first_name":"dada","last_name":"Kara","e_mail":"yaka@gmail","fullname":"Dada Kara","forum_username":"ycan"}]

and i use following code to parse but it doesnt parse it

-(IBAction)loginButton:(id)sender{

    NSString *username = usernameTextfield.text; 
    NSString *password = passwordTextfield.text;

    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:kPostUrl]];
    [request setHTTPMethod:@"POST"];

    NSString *post =[[NSString alloc] initWithFormat:@"e_mail=%@&password=%@", username, password];
    [request setHTTPBody:[post dataUsingEncoding:NSASCIIStringEncoding]];

    NSURLResponse *response;
    NSError *err;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    //NSString *responseStr = [NSString stringWithUTF8String:[responseData bytes]];
    //NSLog(@"Response : %@", responseStr);

    NSString *json_string = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"JSON string : %@", json_string);

    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *responseObj = [parser objectWithString:json_string error:nil];
    NSArray *name = [responseObj objectForKey:@"first_name"];

    NSLog(@"Name : %@", name);
}

The result from my NSLog for name is NULL

Where is the problem and how can I parse such a data so when it comes to lots of rows I can save it to the local FMDB database on iphone

------------------------------EDIT---------------------------------------------------------------

Actual problem was response JSON string from server included echo beginning of the string,json parser only parses between double quotes "", so all i just needed to trim echo from string and parse new string.

and bingo!

//trim in coming echo
    NSString *newString1 = [json_string stringByReplacingOccurrencesOfString:@"correct username and password\n" withString:@""];



    SBJsonParser *parser = [[SBJsonParser alloc] init];

    NSArray *responseObj = [parser objectWithString:newString1 error:nil];

    NSDictionary *dataDict = [responseObj objectAtIndex:0];

    NSString *userID = [dataDict objectForKey:@"user_id"];

    NSLog(@"user_id: %@", userID);

output : user_id : 7

2
  • 3
    The JSON string is an array that contains dictionary, so first extract the dictionary and then parse it. Commented May 14, 2012 at 7:00
  • i guess below answer is the one you have mentioned but i still get NULL Commented May 16, 2012 at 3:39

2 Answers 2

2
 SBJsonParser *parser = [[SBJsonParser alloc] init];

 NSArray *responseObj = [parser objectWithString:json_string error:nil];

NSDictionary *dataDict = [responseObj objectAtIndex:0];

NSString *name = [dataDict objectForKey:@"first_name"];

Did you print recieve data ? is it showing recieve data from server ? If yes then try with different encoding.

Sign up to request clarification or add additional context in comments.

5 Comments

yes NSLog(@"JSON string : %@", json_string); prints JSON string : correct username and password [{"user_id":"7","first_name":"dada","last_name":"Kara","e_mail":"yaka@gmail","fullname":"Dada Kara","forum_username":"ycan"}] what do u mean by different encoding?
NSString *json_string = [[NSString alloc] initWithData:responseData encoding:Some other encoding]; like NSASCIIStringEncoding & other
problem is my json string begins with correct username and password while a json parser only parses between double quotes "" i need to find a way that parser starts after string correct user name and password
You can create your own string with the Help of Nsstring append format and Then send it to Parser.
Thank you so much. It worked like a charm. I was finding solution for this problem from hours. Thank you again :)
0

You can use a tool like Objectify ($15 US) or JSON Accelerator ($0.99 US) in the Mac App store to automatically generate data models for you that would make the model as simple as doing object.firstName.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.