I am receiving a JSON object like this:

{"data":null,
 "error":1,
 "error_code":"InvalidSID",
 "sid":"",
 "num_rows_total":0,
 "last_insert_id":0,
 "error_info":"Comment...",
 "error_data":[]}

and JSONKit using this code:

NSString *responseString = [request responseString];
NSDictionary *requestDictionary = [responseString objectFromJSONString];
if([[requestDictionary objectForKey:@"error"] intValue]) {
    if([@"InvalidSID" isEqualToString:[requestDictionary objectForKey:@"error_code"]]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

produces such output:

{
data = "<null>";
error = 1;
"error_code" = InvalidSID;
"error_data" = ();
"error_info" = "Comment...";
"last_insert_id" = 0;
"num_rows_total" = 0;
sid = "";
}

The problem is, that this if statement is never called because of missing quotation marks around InvalidSID. Is there any known problem with JSONKit that makes those quotation marks disappear?

有帮助吗?

解决方案

You are confusing the "description" output from NSDictionary with the value of a key. You also could have saved yourself a lot of time (that is the time you posted this to the time you get some response) by using some simple detective work.

I assume that what you call "output" above is the result of

NSLog(@"%@", requestDictionary);

So after that line try this:

// Just to be complete
    id ee = [requestDictionary objectForKey:@"error"];
    NSLog(@"error=%@ intValueOfError=%d classOfErrorCode=%@", 
        ee, [ee intValue], NSStringFromClass([ee class]) );

// Where I suspect you may discover something
    id ec = [requestDictionary objectForKey:@"error_code"];
    NSLog(@"errorCode=%@ classOfErrorCode=%@", 
        ec, NSStringFromClass([ec class]) );

We do that since something is obviously wrong here, we want to find out more about the objects we have in hand. I am going to guess if you do the above you will discover something you did not expect.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top