-1

The basic gist: I've got some JSON coming back from a webservice to validate a login; that part works. I'm pulling values out of the array into an NSDictionary; that part works. I need to check one of the values that comes back to know if it was successful or not. That's where it's failing. And as far as I can tell, it's telling me that "success" is not equal to "success".

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];

NSString *result = [jsonArray valueForKey:@"result"];
NSLog(@"%@",result);

if ([result isEqual:@"success"]) {

The log shows "result" is getting set as "success", but it never seems to evaluate as true.

If I set "result" manually:

NSString *result = @"success";

...it gets into the if statement just fine, so it seems like there's something I'm missing that's pointing to a data type or something similar... I'm just at a loss of what else to try at this point.

I'm normally a web dev, but I'm new to iOS, so my debugging is still a little lacking in xcode but I'm familiar with general logic and such. Any help you guys could give me would be fantastic!

Edit:

NSLog showing the JSON coming back from the webservice:

2014-01-10 16:22:42.568 LoginTest[1640:70b] (
    {
        code = 1;
        fname = Joe;
        lname = Tests;
        result = success;
        token = 2555f13bce42b14cdc9e60b923bb2b20;
        vendornum = 50000000;
    }
)

Edit - final working code:

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];

NSLog(@"jsonArray: %@", jsonArray);

NSString *result = [jsonArray[0] objectForKey:@"result"];
NSLog(@"%@",result);

if ([result isEqual:@"success"]) {
Given Reed
  • 11
  • 2
  • 2
    What happens if you put `NSLog(@"result = \"%@\"",result);` that will test to see if there spaces before or after "success". – Jeffery Thomas Jan 10 '14 at 21:19
  • 2
    You should paste the JSON (or at least the relevant part) you're receiving. From your other comment it seems like you may not be getting the exact string you expect but an array instead. – Taum Jan 10 '14 at 21:21
  • 1
    Do an NSLog(@"jsonArray: %@", jsonArray); and see what is output. – David Wong Jan 10 '14 at 21:22

3 Answers3

5

Earlier you commented (from a now removed answer) that -isEqualToString: threw an unrecognized selector. I believe it was -[__NSCFArray -isEqualToString:] or something very similar.

Based on your comment, you don't have "success", you have [ "success" ] in your JSON.

That is an array which wraps the value of a string. You need to get the first element of the array and use that.

[result[0] isEqual:@"success"]

Based on the output in your log, your JSON is not an object

{
  …
  "result" = "success"
  …
}

It is an array with only one object in it.

[
  {
    …
    "result" = "success"
    …
  }
]

You are working with an array of data so the output of -valueForKey: will be an array of data.

@MartinR is correct, it may be clearer to use

[jsonArray[0] objectForKey:@"result"]

to get the result.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Switched "results" to NSArray and used the above line... it worked. I'll approve your solution as soon as it lets me. Any idea why it will let me put an NSArray into an NSString? – Given Reed Jan 10 '14 at 21:28
  • @GivenReed: `[jsonArray valueForKey:@"result"]` applies `valueForKey:` to each element of the array and returns an *array* with the corresponding values. A more straightforward solution would be to set `NSString *result = [[jsonArray objectAtIndex:0 ] objectForKey:@"result"];`, which would be the string that you expect. – Martin R Jan 10 '14 at 21:31
  • @MartinR that is not a good way to put it. It all depends on what your JSON looks like. – Jeffery Thomas Jan 10 '14 at 21:33
  • @JefferyThomas: Exactly. If you look at the NSLog output shown in the question then you see that the outermost element is an *array*. - (`valueForKey:` is frequently abused.) – Martin R Jan 10 '14 at 21:34
2

You didn't show us the actual output of the log. That's bad. Deducing from your comments, it should have shown something like

(
    "success"
)

which is the description of an array object (NSArray) containing a string, rather than the string object itself.

If this is indeed the case, then you need to get the (only? first?) element in the array and compare that using isEqual: or isEqualToString:.

0

if we want to compare two NSString we use [str1 isEqualToString:str2]; You should do same instead of isEqual:

isEqual: compares a string to an object, and will return NO if the object is not a string. do it if you are not sure if object is NSString.

`isEqualToString:` use it when you are sure both Objects are NSString.
Zeeshan
  • 4,194
  • 28
  • 32