Reputation: 181
I am new to iOS. I created a login page and everything works fine. I used JSON for checking username and password and got the response from server in a dictionary format. I want to extract the values from the dictionary and check them in my program. The response which I get from the server is:
json: {
error = 0;
msg = "";
value = {
user = false;
};
};
First I want to check if the value with the key error
is 0
or 1
. Then I want to check the value with the key user
. I don't know how I should code to check it. Can anyone help?
The code which I tried is below:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *respString = [[NSString alloc] initWithData:loginJSONData encoding:NSUTF8StringEncoding];
SBJsonParser *objSBJSONParser = [[SBJsonParser alloc] init];
NSDictionary *json = [[NSDictionary alloc] initWithDictionary:[objSBJsonParser objectWithString:respString]];
NSLog(@"json: %@",json);
NSString *error = [json objectForKey:@"error"];
NSLog(@"error: %@", error);
if ([error isEqualToString:@"o"])
{
NSLog(@"login successful");
}
else
{
NSLog(@"login fail");
}
}
Upvotes: 13
Views: 46017
Reputation: 1415
The dictionaries that normally returning from the server will be as a key value pair. if you are looking for accessing these values that corresponds to a key, then these code may help you
NSString *varname = [[NSString alloc]initWithFormat:@"%@",[dictionaryname objectForKey:@"key"]];
Upvotes: 2
Reputation: 1840
Using modern Objective-C, accessing things in arrays and dictionaries become easier.
You should use the following syntax:
id<NSObject> value = dictionary[@"key"];
Similarly,
id<NSObject> value = array[1]; // 1 is the index
Applying the above to the question:
NSString *error = json[@"error"];
NSDictionary *value = json[@"value"];
BOOL user = [json[@"value"][@"user"] boolValue];
As in the above line, nesting is allowed, but it is not a good practice.
Upvotes: 18
Reputation: 6445
You can use the below code
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *respstring = [[NSString alloc]initWithData:loginJsonData encoding:NSUTF8StringEncoding];
NSDictionary *dic = [responseString JSONValue];
NSLog(@"%@",dic);
NSNumber *error = [dic objectForKey:@"error"];
if([error intValue] == 0) {
NSLog(@"login successful");
}
else
{
NSLog(@"login fail");
}
NSString *user = [value objectForKey:@"user"];
BOOL userStatus = [user boolValue];
}
Upvotes: 0
Reputation: 39978
NSNumber *error = [json objectForKey:@"error"];
if ([error intValue] == 0)
{
NSLog(@"login successful");
NSDictionary *value = [json objectForKey:@"value"];
NSNumber *user = [value objectForKey:@"user"];
if ([user boolValue])
{
NSLog(@"user == true");
}
else
{
NSLog(@"user == false");
}
}
else
{
NSLog(@"login failed");
}
Upvotes: 5
Reputation: 47049
For get error value from your JSON
Dictionary.
NSString *error = [myJSONDicName objectForKey:@"error"];
For get user value from your JSON
Dictionary.
NSString *error = [[myJSONDicName objectForKey:@"value"] objectForKey:@"user"];
EDITED:
You just need to change In your
if ([error isEqualToString:@"o"])
_^_
|
change 'o'
to '0'
Upvotes: 1