2

我正在通过 Mashape.com 使用一个名为 Lamda (https://www.mashape.com/lambda) 的人脸识别 API。

当我从识别请求中得到结果时,它给了我一个“MashapeRespose”对象。

我无法将此 MashapeResponse 或其任何部分转换为 JSON 对象。

当我打电话

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

我把这个打印出来

{
    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;
} 

显然这不是有效的 JSON。

这是 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 

有没有人能够在 iOS 上使用 Mashape 并能够转换为 JSON 对象?

我可以使用子字符串来获取我想要的信息,但这不是它应该如何工作,因为文档声明它应该是一个 json 响应..

至少可以说非常沮丧...

问候达米安

PS。我已经向 Mashape 支持发送了这个问题,所以如果我在有人在这里发布答案之前得到了解决方案,我会将它添加到这篇文章中。

编辑:

当我打电话

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

我得到了这个例外

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.'
4

2 回答 2

2

首先感谢 Dcritelli 对这个问题的帮助。

我收到了 Mashape 关于我的问题的回复,他们给我发了这封电子邮件作为回复。


嗨,达米安,

[response body] 方法已经返回一个解析的 JSON 对象。如果您需要以字节为单位的原始响应(即 NSData* 对象),您可以通过调用 [response rawBody] 来获取它。如果需要,您可以将 NSData* 转换为 NSString*。

假设 [response body] 返回解析后的 JSON 对象,例如要获取“photos”数组中第一张照片的“url”属性,您可以编写:

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

如果文档令人困惑,我们深表歉意,我们对其进行了更新,您可以在此处阅读新修订: https ://www.mashape.com/docs/consume/objectivec

如果您需要其他帮助,请告诉我们,干杯


因此,他们不仅回复了解决方案,还更新了文档。

向 Mashape 致敬。

问候达米安

于 2012-09-21T09:08:11.657 回答
1

我相信这是一个字典对象。api描述中应该列出键。只需使用:

[response valueForKey:@"key"]

并将值分配给适当的对象:数组、字典或任何它碰巧的对象。

于 2012-09-20T15:31:24.947 回答