-1

I use the following code to create a dictionary based on a JSON string received from the server. (I have downloaded JSONKit and embedded it into the project). The code below returns a legal JSON string from the server (parsed well on Android) but crashes when I try to convert it to a dictionary.

- (IBAction)submit
{
bool useSSL = true;
char *c_url="http://(rest of URL)";


NSString* url = [NSString stringWithFormat:@"%s" , c_url];
url = [NSString stringWithFormat:@"%@%@%s", url, self.label.text, "/keys"];
NSString * response = [self getDataFrom:url];


NSDictionary *dict = [response objectFromJSONString]; //generates SIGABRT!!
NSLog(@"%@",dict); 

NSString *success = [dict valueForKey:@"success"];

} 



- (NSString *) getDataFrom:(NSString *)url{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:url]];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;

NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

if([responseCode statusCode] != 200){
    NSLog(@"Error getting %@, HTTP status code %i", url, [responseCode statusCode]);
    return nil;
}

return [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding]; 
}

THANKS, Simon

Simon
  • 509
  • 7
  • 25
  • 1
    Can you try put NSLog(@"%@",response); before `NSDictionary *dict = [response objectFromJSONString];` and see what is the output. – iphonic Aug 20 '13 at 07:36
  • 1
    instead of using the 3rd party json library, y dont u use the nsjsonserialization(above iOS5)....here is the link....http://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization – Pradeep Aug 20 '13 at 07:38
  • Seems like your JSON response is incorrect – Injectios Aug 20 '13 at 07:49
  • I build for 4 and up so I cannot use the native library. The output of the NSLOG: {"key":"1234","address":"http:\/\/www.mishkanothamada.com","success":"true","type":"keys","method":"keys"} – Simon Aug 20 '13 at 07:58
  • What error do you see in Xcode when your app crashes? – Daniel S. Jan 29 '14 at 14:29
  • In which line does your app crash? – Daniel S. Jan 29 '14 at 14:29

1 Answers1

0

Found an answer here. Answer says: "Figured it out... I had JSONKIt.h included in the project but for some weird reason, JSONKit.m was not included in the 'Compile Sources' under 'Build Phases' - once I added it manually it started working fine."

Community
  • 1
  • 1
Simon
  • 509
  • 7
  • 25