Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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) {
......
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top