13

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");
    }
}
Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
Saba Sayed
  • 181
  • 2
  • 4
  • 11

5 Answers5

18

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.

Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
cream-corn
  • 1,820
  • 14
  • 26
  • i tried above code but its throwing exception. can u please tell how to code for user for getting the values "true" or "false". – Saba Sayed Nov 14 '13 at 06:37
  • so the first segment `json[@"value]` returns us the NSDictionaryObject containing the user attribute then accessing `json[@"value"][@"user"]` will return us the boolean as a NSNumber object (at least i think) then calling `BoolValue` on that will convert into a BOOL... I suspect that the exception is happening when we try to call `boolValue`, could you paste the exception? – cream-corn Nov 14 '13 at 06:43
  • [__NSDictionaryM boolValue]: unrecognized selector sent to instance 0x756a050 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM boolValue]: unrecognized selector sent to instance 0x756a050' libc++abi.dylib: terminate called throwing an exception – Saba Sayed Nov 14 '13 at 06:57
  • Try these in isolation to see if the individual calls work; try `json[@"value"][@"user"]` and see what type the object is. – cream-corn Nov 14 '13 at 23:05
5
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");
}
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • 1
    The code for `user` is incorrect. `json` does not have a `user` key. – rmaddy Nov 14 '13 at 05:32
  • Move the code to get the user inside the "then" portion of the `if` statement and you have the right answer. – rmaddy Nov 14 '13 at 05:41
  • @rmaddy I don't have exact idea where he wants to write the code to check `user` value but inside `if` it's the most appropriate. Thanks for the suggestion – Inder Kumar Rathore Nov 14 '13 at 05:44
  • I made the suggestion because that is what I thought the OP stated but after reading again I see that it is unclear. But it makes sense to only check the user if there is no error. – rmaddy Nov 14 '13 at 05:45
2

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"]];
Nagarjun
  • 6,557
  • 5
  • 33
  • 51
SARATH SASI
  • 1,395
  • 1
  • 15
  • 39
1

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'

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Yes, the OP does. But the OP is incorrect. That's one of the reason the OP's code doesn't work. – rmaddy Nov 14 '13 at 05:37
  • @rmaddy - but in the case of OP.. he/she only need to check error is `0` or `1` then i think `isEqualToString` is work. – iPatel Nov 14 '13 at 05:39
  • But the code will crash since `NSNumber` doesn't have an `isEqualToString:` method. – rmaddy Nov 14 '13 at 05:40
  • @rmaddy - `NSNumber`??? sorry but OP got it as NSString so not need to use `NSNumber`.. – iPatel Nov 14 '13 at 05:41
  • As I said, the OP is wrong. Look at the JSON. The `0` is not in quotes, is not an `NSString`. – rmaddy Nov 14 '13 at 05:43
0

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];
 }
manujmv
  • 6,450
  • 1
  • 22
  • 35