0

I am getting response form api like this.

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6ImpvaG4uc21pdGhAZ21haWwuY29tIiwiaWQiOiI1NzFkYzI3NmU0YjA1NjVmNTcwZjM2ZGQiLCJpYXQiOjE0NjMwMjk4NDd9.yi8H75GTS-U8abcS75WcGT5ROfmM0AgCNfRIiZQzeNI","data":{"name":"John Smith","role":"driver"},"message":"success"} 

But when I try to get value of token it gives me null at the same time when get value of role it give me perfect value.

Please look at my code.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://qa.networc.in:1336/api/drivers/login"]]; NSURLSession *session = [NSURLSession sharedSession]; NSString * params =[NSString stringWithFormat:@"email=%@&password=%@&deviceId=fc2ffdf08ccf0dc912018f7232e4aa0ffcbd856ec3faf4145649f8bb281a779d",_driverNumberTextField.text,_passwordTextField.text]; NSLog(@"params %@", params); request.HTTPMethod = @"POST"; request.HTTPBody =[params dataUsingEncoding:NSUTF8StringEncoding]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"Response:%@ %@\n", response, error); if(error == nil) { // use NSJSON Serlizeitaion and serlize your value NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog(@"Data = %@",text); id object = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"JSON"]; NSString *temp; temp = (NSString*)[dictionary valueForKey:@"token"]; NSLog(@"temp %@", temp); dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"data"]; Role = (NSString*)[dictionary valueForKey:@"role"]; NSLog(@"role %@", Role); 
7
  • There are many post related to JSON parsing....try to find out that and don't ask similar question. Commented May 12, 2016 at 5:19
  • 1
    You have made a mistake instead of dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"JSON"]; use dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; Commented May 12, 2016 at 5:20
  • where i make mistake sir? Commented May 12, 2016 at 5:23
  • remove the objectForKey:@"JSON" part as there is no key such as"JSON". Commented May 12, 2016 at 5:26
  • what the text u get here Commented May 12, 2016 at 5:26

6 Answers 6

2

you need to do like

dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSString *temp = (NSString*)[dictionary valueForKey:@"token"]; NSLog(@"temp %@", temp); NSDictionary *data = [dictionary valueForKey:@"data"]; NSString *tnamemp = (NSString*)[data valueForKey:@"name"]; NSString *role = (NSString*)[data valueForKey:@"role"]; 
Sign up to request clarification or add additional context in comments.

Comments

1

//i hope it will work for u

 NSMutableDictionary *dic= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSString *strToke = [dic valueForKey:@"token"]; 

Comments

1

NSJSONSerialization will return dictionary so you don't need to take objectForKey:@"JSON"

replace

dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"JSON"]; 

with

dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

Suggestion : Do not parse dictionary again just to retrieve different keys from same dictionary. Use same dictionary.

NSString *token = dictionary[@"token"]; NSString *role = dictionary[@"data"][@"role"]; NSString *name = dictionary[@"data"][@"name"]; 

Comments

0

Please try out the below code: The problem is here:

dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:@"JSON"]; 

try out the below code. Hope this helps!

NSError *error = nil; NSDictionary *responseObj = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; if(! error) { NSLog(@"responseObj : %@",responseObj); NSString *token = [responseObj valueForKey:@"token"]; NSDictionary *dataDic = [responseObj valueForKey:@"data"]; NSString *role = [dataDic valueForKey:@"role"]; NSString *name = [dataDic valueForKey:@"name"]; } else { NSLog(@"Error in parsing JSON"); } 

Comments

0

There is problem with fetching keys Try this,

 NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSString *strToken = (NSString*)[dataDictionary valueForKey:@"token"]; NSLog(@"Token %@", strToken); NSString *strName = (NSString*)[[dataDictionary valueForKey:@"data"] valueForKey:@"name"]; NSString *strRole = (NSString*)[[dataDictionary valueForKey:@"data"] valueForKey:@"role"]; 

Comments

-1

Print out single values and check. Access array $arr["token"]

1 Comment

sir i think its in dictionary form not in array?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.