简体   繁体   中英

acces JSON from server side

I have the following response from server:

{"_license":false}

And when I try to get out from there _license it displays null .I tried like this:

NSString *items = [[parser objectWithString:[request responseString] error:nil] valueForKey:@"_license"];
NSLog(@"response server%@", items);

Any idea why?And how to solve?

Try en spilt up your code, don't do every thing in one line. Then check ever variable if it contains the correct value.

NSLog(@"Server response: %@",[request responseString]);
NSError * error = nil;

NSDictionary *response = [parser objectWithString:[request responseString] error:&error]

if (!response)( {
  NSLog( @"Error parsing JSON: %@", error);
  return;
}

NSLog(@"Dictionary: %@", response);

NSNumber *hasValidLicense = [response objectForKey:@"_license"];
NSLog(@"Has valid license: %@", hasValidLicense);

if ([hasValidLicense boolValue]){
   //Yes we have a valid license.
} else {
   // No valid license.
}

用以下内容替换您的行:

BOOL items = [[[parser objectWithString:[request responseString] error:nil] valueForKey:@"_license"] boolValue];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM