JSONparsing器返回Null

我想学习如何parsingJSON数据,所以我可以处理大型数据库。 我写了代码login到一个网站。

我有一个成功的login请求下面的JSON数据:

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

我使用下面的代码parsing,但它不parsing它

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

我的名字的NSLog的结果是NULL

问题在哪里,我怎么能parsing这样的数据,所以当涉及到很多行我可以保存到本地FMDB数据库的iPhone

– – – – – – – – – – – – – – – 编辑 – – – – – – – – – – ——————————————–

实际的问题是响应JSONstring从服务器包括回声开始的string,JSONparsing器只parsing双引号“”,所以我只需要修剪从string回声和parsing新的string。

和宾果!

 //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); 

输出:user_id:7

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

你打印接收的数据? 它显示从服务器接收数据? 如果是,那么尝试不同的编码。

您可以在Mac App商店中使用像Objectify($ 15 US)或JSON Accelerator($ 0.99 US)这样的工具来为您自动生成数据模型,从而使模型与执行object.firstName一样简单。