Search code examples
iosobjective-clambdamashape

Mashape Objective-C for iOS - MashapeResponse to JSON Object


I am consuming an API through Mashape.com called Lamda (https://www.mashape.com/lambda) a face recognition API.

When I get my result back from the recognize request it gives me a "MashapeRespose" object.

I cannot convert this MashapeResponse or any of its parts into a JSON object.

When I call

NSLog(@"des=%@", [response body]);

I get this printed out

{
    images =     (
        "http://api.lambdal.com/static/uploads/img05Xv8N.png"
    );
    photos =     (
                {
            height = 241;
            tags =             (
                                {
                    attributes =                     (
                                                {
                            confidence = "0.7097208087892503";
                            gender = male;
                        }
                    );
                    center =                     {
                        x = 95;
                        y = 98;
                    };
                    confidence = "0.978945010372561";
                    "eye_left" =                     {
                        x = 114;
                        y = 84;
                    };
                    "eye_right" =                     {
                        x = 76;
                        y = 85;
                    };
                    height = 112;
                    "mouth_center" =                     {
                        x = "96.5";
                        y = "134.5";
                    };
                    "mouth_left" =                     {
                        x = 74;
                        y = "134.5";
                    };
                    "mouth_right" =                     {
                        x = 119;
                        y = "134.5";
                    };
                    nose =                     {
                        x = 94;
                        y = 113;
                    };
                    tid = 31337;
                    uids =                     (
                                                {
                            confidence = "0.323";
                            prediction = manuel;
                            uid = "manuel@MICClientele";
                        },
                                                {
                            confidence = "0.194";
                            prediction = olivier;
                            uid = "olivier@MICClientele";
                        },
                                                {
                            confidence = "0.161";
                            prediction = sean;
                            uid = "sean@MICClientele";
                        },
                                                {
                            confidence = "0.161";
                            prediction = damien;
                            uid = "damien@MICClientele";
                        },
                                                {
                            confidence = "0.065";
                            prediction = egle;
                            uid = "egle@MICClientele";
                        },
                                                {
                            confidence = "0.065";
                            prediction = gareth;
                            uid = "gareth@MICClientele";
                        }
                    );
                    width = 112;
                }
            );
            url = "http://api.lambdal.com/static/uploads/img05Xv8N.png";
            width = 200;
        }
    );
    status = success;
} 

Obviously this is not valid JSON.

Here is the contents of MashapeResponse.h

#import "MashapeResponse.h"

@interface MashapeResponse ()
@property (readwrite, retain) NSData* rawBody;
@property (readwrite) NSDictionary* headers;
@property (readwrite) int code;
@end

@implementation MashapeResponse

@synthesize rawBody;
@synthesize headers;
@synthesize code;
@synthesize body;

-(MashapeResponse*)initWithResponse:(int)httpCode headers:(NSDictionary*) httpHeaders rawBody:(NSData*) httpRawBody {
    self = [super init];
    [self setCode:httpCode];
    [self setHeaders:httpHeaders];
    [self setRawBody:httpRawBody];
    return self;
}

@end 

Has anyone been able to use Mashape on iOS and been able to convert into a JSON object?

I can use substring to get the info I want but this is not how it should work since the docs state its should be a json response..

Very frustrated to say the least...

Regards Damien

PS. I have mailed Mashape support with this question so if I get a solution back before someone posts an answer here i will add it to this post.

Edit:

When I call

id status = [response valueForKey:@"status"];
NSLog(@"status=%@", [status description]);
id photos = [response valueForKey:@"photos"];
NSLog(@"photos=%@", [photos description]);

I get this exception

2012-09-20 16:53:20.609 Clientele[5160:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MashapeJsonObjectResponse 0x3d2810> valueForUndefinedKey:]: this class is not key value coding-compliant for the key status.'

Solution

  • Firstly thanks you Dcritelli for your help with this issue..

    I got a reply from Mashape about my issue and they sent me this email in reply.


    Hi Damien,

    The [response body] method already returns a parsed JSON object. If you need the raw response in bytes (that is a NSData* object), you can get it by invoking [response rawBody]. You can convert a NSData* to NSString* if you want.

    Given that [response body] returns the parsed JSON object, for example to get the "url" property of the first photo in the "photos" array, you would write:

     NSArray* photos = [[response body] objectForKey:@"photos"];
     NSDictionary* firstPhoto = [photos objectAtIndex:0];
     NSString* url = [firstPhoto objectForKey:@"url"];
     NSLog(@"URL of the first photo is: %@", url);
    

    We apologize if the docs were confusing, we updated them and you can read the new revision here: https://www.mashape.com/docs/consume/objectivec

    Let us know if you need additional help, Cheers


    So not only have they replied with a solution they have also updated their documentation.

    Hats off to Mashape.

    Regards Damien