Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get an integer from JSON

im parsing a JSON and cant seem to extract an integer. If i do

  int  secondsLeft = [secondsList objectForKey:@"SecondsToStop"];

if I do NSLog(@"%@",secondsLeft) it does however get outputted correctly in the console, but im unsure how to get a proper integer. Whats the proper way to parse numbers from a JSON object?

like image 415
jfisk Avatar asked Jan 20 '12 02:01

jfisk


People also ask

Can JSON return integer?

Strictly speaking, json is untyped, so you can't send it as an integer, it has to be a string.

Can integer be key in JSON?

JSON only allows key names to be strings. Those strings can consist of numerical values.


1 Answers

Your JSON parsing framework probably stored the number as an NSNumber.

According to the NSNumber documentation, you can call intValue on the number to get its value as a plain old int:

int secondsLeft = [[secondsList objectForKey:@"SecondsToTop"] intValue];
like image 82
s4y Avatar answered Oct 08 '22 21:10

s4y