问题
Doc Ref: http://docs.aws.amazon.com/apigateway/latest/developerguide/models-mappings.html
In AMS VTL one specifies dictionary fields in a model schema thus:
"field1" : {"type":"string"},
"field2" : {"type":"number"},
and so a mapping template can populate such fields thus:
#set($inputRoot = $input.path('$'))
"questions" :
[
#foreach($elem in $inputRoot)
{
"field1" : "$elem.field1",
"field2" : $elem.field2
}#if($foreach.hasNext),#end
#end
]
However... my iOS app complains the received data isn't in JSON format. If I add quotes around $elem.field2 then iOS accepts the JSON and converts all fields to strings.
My Lambda function is returning is returning a standard JSON list of dictionaries with field2 defined as an integer.
But APIG returns strings for all my fields, delimited with {} and a prefix:
{S=some text}
{N=10000000500}
So I can see that field2 isn't a number but a string {N=10000000500}.
How do I handle numbers in this system?
回答1:
Undocumented but you can simply specify the type after the field name in a mapping template:
#set($inputRoot = $input.path('$'))
"questions" :
[
#foreach($elem in $inputRoot)
{
"field1" : "$elem.field1.S",
"field2" : $elem.field2.N
}#if($foreach.hasNext),#end
#end
]
Note that string fields need to be delimited in quotes.
来源:https://stackoverflow.com/questions/35743522/specifying-numbers-in-vtl-on-ams-api-gateway