Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON parser returns Null
    primarykey
    data
    text
    <p>I am trying to learn how to parse JSON data so I can handle big databases. I wrote code to login into a website.</p> <p>I have following JSON data from a successful login request:</p> <pre><code>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"}] </code></pre> <p>and i use following code to parse but it doesnt parse it</p> <pre><code>-(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=%@&amp;password=%@", username, password]; [request setHTTPBody:[post dataUsingEncoding:NSASCIIStringEncoding]]; NSURLResponse *response; NSError *err; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;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); } </code></pre> <p>The result from my <code>NSLog</code> for name is <code>NULL</code> </p> <p>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</p> <p><strong>------------------------------EDIT---------------------------------------------------------------</strong></p> <p>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.</p> <p>and bingo!</p> <pre><code>//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); </code></pre> <p>output : user_id : 7</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload