如何从 iOS 访问 AWS API 网关模型

How to access AWS API Gateway Models from iOS

AWS 模型架构

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "QuestionsModel",
    "type": "array",
    "items": {
    "type": "object",
    "properties": {
        "placeholder" : { "type":"string" },
        "type" : { "type":"string" },
        "order": { "type": "integer" },
        "prompt": { "type": "string" },
        "section_name": { "type": "string" }
        }
    }
}

AWS 集成响应 - 映射模板 - application/json

使用 Velocity 模板语言进行映射 数组...

#set($inputRoot = $input.path('$'))
[
#foreach($elem in $inputRoot)
{
  "type" : "$elem.type",
  "placeholder" : "$elem.placeholder",
  "order" : "$elem.order",
  "prompt" : "$elem.prompt",
  "section_name" : "$elem.section_name"
} 
#if($foreach.hasNext),#end
#end
]

AWS Lambda 函数

def lambda_handler(event, context):
    client = boto3.client('dynamodb')

    response = client.scan(
        TableName='Question',
        AttributesToGet=[
            'type',
            'order',
            'section_name',
            'prompt',
            'placeholder'])

    return = response['Items']

iOS 应用模型

iOS 模型有一个 NSString 类型的字段 type,填充了值 {S=Hello World}

我宁愿 iOS 字段等于 Hello World 省去解析 {S=*}

我哪里错了?

你有没有在方法响应中设置响应模型? 这是 API Gateway 提供的基本演练。 http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-models.html

我在 中确定了答案。

未记录,但您可以在映射模板中的字段名称后简单地指定类型:

#set($inputRoot = $input.path('$'))
[
#foreach($elem in $inputRoot)
{
  "field1" : "$elem.field1.S",
  "field2" : $elem.field2.N
}#if($foreach.hasNext),#end
#end
]

请注意,字符串字段需要用引号分隔,而数字不需要。