0

I am using a singleton class to fetch JSON from a remote server (via NSURLConnection) - everything seeme to be fine except when I try to parse the JSON using JSONKit.

Here is some code

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
 [apiData appendData:data];  
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse   *)response
{
NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response;
if (realResponse.statusCode == 200)
{
    apiData = [[NSMutableData alloc] init];
} else {
    NSLog(@"Bad response = %i",realResponse.statusCode);
}
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonData = [[NSString alloc] initWithData:apiData encoding:NSUTF8StringEncoding];
NSDictionary *deserializedData = [jsonData objectFromJSONString];
[self.delegate dataLoaded:deserializedData]; 
}

The error I get is at this line

 NSDictionary *deserializedData = [jsonData objectFromJSONString];

-[__NSCFString objectFromJSONString]: unrecognized selector sent to instance 0x7fc1cd0

Any ideas what is going on here? This seems to be the normal way to parse JSON using JsonKit.

I have already made sure that JSON is valid...Does the string get corrupted somehow during appending in didReceiveResponse?

Anuj Gakhar
  • 681
  • 2
  • 13
  • 26
  • Possible duplicate of [objectFromJSONString crash when launching app that uses Simperium](http://stackoverflow.com/questions/10478030/objectfromjsonstring-crash-when-launching-app-that-uses-simperium) – Jay O'Conor May 22 '12 at 15:51
  • I already have -ObjC in other linker flags – Anuj Gakhar May 22 '12 at 15:55
  • 2
    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. – Anuj Gakhar May 22 '12 at 16:01
  • can someone close this question please? – Anuj Gakhar May 22 '12 at 16:02
  • Just post your answer as an answer and then mark it the next day. – James Jul 25 '12 at 23:00

1 Answers1

8

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.

Anuj Gakhar
  • 681
  • 2
  • 13
  • 26