我使用AWS API Gateway定义了我的REST API并为iOS生成了客户端代码。当我调用方法时,SDK会输出以下错误消息:
AWSiOSSDKv2 [Error] AWSAPIGatewayClient.m line:190 __118-[AWSAPIGatewayClient invokeHTTPRequest:URLString:pathParameters: queryParameters:headerParameters:body:responseClass:]_block_invoke_2 | Failed to serialize the body JSON. (null)
有什么问题?
答案 0 :(得分:1)
容易!
确保您的AWSModel具有与JSON密钥路径属性键数相同的类成员数。我没有班级成员和2个属性键。
确保每个属性键的名称与类成员的名称相匹配。我再次获得了#34;代码"没有匹配的#34;代码"属性。
为清楚起见,请查看JSONKeyPathsByPropertyKey函数。如果你看到@"abc": @"def"
那么你必须拥有一个属性" abc"在您的类中,否则JSON转换将失败。
// Sample JSON returned by AWS API Gateway
{"code":200, "message":"OK", "data":{"phone":"(555) 555-1234"}}
// APISample.h
#import
#import
@interface APISample : AWSModel
// We count 4 class members
@property (nonatomic, strong) NSNumber *code;
@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) NSDictionary *data;
@property (nonatomic, strong) NSNumber *phone;
@end
// APISample.m
#import "APISample.h"
@implementation APISample
// We count 4 property keys
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"code": @"code",
@"message": @"message",
@"data": @"data",
@"phone": @"data.phone"
};
}
提示:请注意如何访问分支(数据为NSDictionary)并使用点表示法(data.phone)遍历文档结构。
Bonus:一个适合您的Swift示例。
// Swift sample code to access AWS API Gateway under iOS
// Create a client with public access
var client : APISampleClient = APISampleClient.defaultClient()
// Comment next line if your API method does not need API key
client.APIKey = "Your API key"
client.SampleMethodGet().continueWithBlock { (task : AWSTask) -> AnyObject? in
if task.error != nil {
print("Error \(task.error)")
}
else if task.result != nil {
let output = task.result as! APISample
print("Success \(output)")
}
return nil
}