Question

I'm using SBJson to parse some JSON result from a webservice. The problem is I'm not sure how SBJson handles boolean types. The service returns it as a true or false value; is this handled automatically in SBJson or do I have to detect it myself?

Était-ce utile?

La solution

Might I suggest you check out the class documentation: http://json-framework.googlecode.com/svn/trunk/documentation/interfaceSBJSON.html

I believe SBJson returns booleans as NSNumbers set to either 0 or 1 which you can use a boolean values for things like if statements. Or you could always just get the boolValue for a true BOOL type

Autres conseils

Coming from Java, this confused me too (both lack of a real Boolean and how SBJson represents); 2nd BOOL example, of course, doesn't work:

BOOL      bDir = ((NSNumber*)[obj objectForKey:@"isDirectory"]).intValue;
//BOOL    bDir = [obj objectForKey:@"isDirectory"];
type           = bDir ? MI_DIRECTORY : MI_FILE;

It's working as @cpjolicoeur mentioned.

Working example:

NSDictionary *response = [responseString JSONValue]
BOOL example = [[response objectForKey:@"example"] boolValue]

if(example) {
......
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top