本文介绍了在AMS API网关上的VTL中指定数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档参考: http://docs.aws.amazon .com/apigateway/latest/developerguide/models-mappings.html

在AMS VTL中,人们在模型架构中指定了字典字段,因此:

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
]

但是...我的iOS应用程序抱怨收到的数据不是JSON格式.如果我在$elem.field2周围加上引号,则iOS接受JSON并将所有字段转换为字符串.

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.

我的Lambda函数返回的是返回标准JSON字典列表,其中field2定义为整数.

My Lambda function is returning is returning a standard JSON list of dictionaries with field2 defined as an integer.

但是APIG返回我所有字段的字符串,以{}和前缀定界:

But APIG returns strings for all my fields, delimited with {} and a prefix:

{S=some text}
{N=10000000500}

所以我可以看到field2不是数字,而是字符串{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?

推荐答案

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

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.

这篇关于在AMS API网关上的VTL中指定数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 14:35