Search code examples
iosobjective-cjsonparsingnsnumber

JSON NSNumber returning incorrectly


I am starting out with JSON with Objective-C, I have retrieved data from the server and am being given an NSDictionary as expected.

As an example, here are two values in my dictionary.

length and start.

length returns as expected, an NSNumber, 268. However, start also apparently an NSNumber (which I would expect it to be though) is chucking a great long string of numbers, as though it isn't formatting properly.

E.g. start = 1423113951.

How can I be certain of the data type I am receiving? That it is getting parsed correctly? And how I should handle it on my end?

I know that start is a number and it should return similarly to length but the long number I get is almost the same every runtime, so it clearly isn't formatting properly.

I get these value by simply logging the NSDictionary I get back from:

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

Some extra detail, here is my request for the data:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://secretAddress"]];
        NSData *data = [NSData dataWithContentsOfURL:url];

        NSError *error;
        NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSLog(@"%@", json);

Understandably it could potentially be how the server is sending it back to me, but currently I do not know how it is being sent.


Solution

  • Whenever I've had issues parsing JSON data, the first thing I'd do is to put a breakpoint on the line of code where I'm parsing the number, and examine the contents of my NSDictionary item:

    enter image description here

    As you can see in this example, XCode has reported that the NoteID from my JSON web service is an NSNumber, but it's happy to let me extract its value into an int.

    So, start there: check the contents of your web service's data, and see if that points you in the right direction.