1

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?

Michal
  • 15,429
  • 10
  • 73
  • 104
  • see this may it help u http://stackoverflow.com/questions/9012942/parsing-json-response-with-multiple-objects – Shruti Aug 03 '12 at 12:08
  • 1
    I am not quite sure how should it help me as I have only "one-level" JSON object.. – Michal Aug 03 '12 at 12:29

1 Answers1

0

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.

David H
  • 40,852
  • 12
  • 92
  • 138
  • I did not call NSLog, but I believe it is the same as calling `po requetDictionary` while holding a breakpoint. The thing was, that the parser is actually fine, it is the console printing funky stuff. My program was actually crashing because of popping view controller BEFORE it appeared, not because that `if` wouldn't happen(I overlooked misplaced breakpoint, hence - "if statement is never called because of missing quotation marks"). But thanks for interesting response. – Michal Aug 03 '12 at 13:34