iosjsonobjective-cinstapaper

How to process response from Instapaper API?


I make a request to the Instapaper API, and it's supposed to return JSON. It returns something close to JSON, but not completely, like follows:

2013-05-30 19:54:20.155 --[53078:c07] (
        {
        type = meta;
    },
        {
        "subscription_is_active" = 1;
        type = user;
        "user_id" = --;
        username = "--@gmail.com";
    },
        {
        "bookmark_id" = 387838931;
        description = "";
        hash = YHwQuwhW;
        "private_source" = "";
        progress = 0;
        "progress_timestamp" = 0;
        starred = 0;
        time = 1369954406;
        title = "Adobe Finally Releases Kuler Color-Picking App for iPhone - Mac Rumors";
        type = bookmark;
        url = "http://www.macrumors.com/2013/05/30/adobe-finally-releases-kuler-color-picking-app-for-iphone/";
    },

How do I then process this? Can I take it and turn it into an NSDictionary even though it doesn't seem to be valid JSON?


Solution

  • From Instapaper API Docs:

    Instapaper strings are always encoded in UTF-8, and Instapaper expects all input to be in UTF-8. Unless otherwise noted, output from every method is an array. The output array is returned as JSON by default. You can specify a jsonp parameter with a callback function name, e.g. jsonp=myCallback, to use JSONP and wrap the output in a call to the specified function.

    So there is no way you will get not valid JSON!

    Try following code:

    NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL urlWithString:@"http://your-instapeper-API-link"] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    id serializationJSON = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    

    And then you can log what is wrong or if result is what you expect:

    NSLog(@"class of JSON input: %@ \n and possible error: %@",[serializationJSON class],error);
    

    Of course you should expect Array and no error.

    EDIT ... based on coment code:

    Based on docs you should get Array or Dictionary. Please add this core instead your line #23 (numer from here):

    if([JSON isKindOfClass:[NSDictionary class]]) {
       NSDictionary *jsonDictionary = JSON;
       NSLog(@"%@",[jsonDictionary allKeys]);
    } else { 
      NSLog(@"JSON object class: %@",[JSON class]);
    }
    

    and please show us output.

    One more thing:

    You get array from request. Great! This is a valid JSON. So you need to debug it. As i said it's a shame is not a unlimited acccess public API, so i can look into it. But now you have to debug your result. I see in your code that you are trying to access bookmarks. So i look into Bookmarks section in docs and this is some kind of list (NSArray). So if you don't know what result you want. You should print them into log (or set a breakpoint). Replace code from my earlier update with this simple log:

    NSDictionary *resultDictionary;
    if([JSON isKindOfClass:[NSArray class]]) {
      NSArray *jsonArray = JSON;
      NSLog(@"so json is an array with %i objects",[jsonArray count]);
      for(id objectInsideArr in jsonArray) {
         NSLog(@"object in array [class]: %@ [value]: %@",[objectInsideArr class],objectInsideArr); //if here you find NSDictionary maybe is this dictionary you are looking for. I'm not sure what it is.
       if([objectInsideArr isKindOfClass:[NSDictionary class]]) {
           resultDictionary = [[NSDictionary alloc] initWithDictionary:objectInsideArr];
       }
      }
    }