如何在 VTL 中处理 AWS APIG 映射模板中的嵌套列表
how to handle nested lists in AWS APIG Mapping Template in VTL
(这是我的模型方案:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "QuestionsModel",
"type": "array",
"items": {
"type": "object",
"properties": {
"section_name": { "type": "string" },
"options" : {
"type" : "array",
"items" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
}
这是映射模板:
#set($inputRoot = $input.path('$'))
[
#foreach($question in $inputRoot) {
"section_name" : "$question.section_name.S",
"options" : [
#foreach($items in $question.options.L) {
[
#foreach($item in $items.L) {
"$item.S"
}#if($foreach.hasNext),#end
#end
]
}#if($foreach.hasNext),#end
#end
]
}#if($foreach.hasNext),#end
#end
]
尽管此语法正确地映射了数据,但它导致 "options" 成为一个空数组。
如果没有指定 "options",那么我的 iOS 应用会收到有效的 JSON。但是,当我尝试 "options" 的各种语法时,我会得到无效的 JSON 或 "Internal Service Error" 并且 CloudWatch 提供的 Unable to transform response
.
也好不了多少
有效选项使用以下内容填充:{L=[{"L":[{"S":"1"},{"S":"Dr"}]},{"L":[{"S":"2"},{"S":"Mr"}]},{"L":[{"S":"3"},{"S":"Ms"}]},{"L":[{"S":"4"},{"S":"Mrs"}]},{"L":[{"S":"5"},{"S":"Prof."}]}]}
,由 Lambda 函数提供。
此时我只能得出结论,API Gateway VTL 不支持嵌套数组。
AWS iOS SDK for Modeling 不支持数组的数组。
您必须在任何嵌套数组之间定义一个字典。
因此,而不是 array/object/array/array 你滑入一个额外的 "awshack" 对象:array/object/array/awshack-object/array
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "QuestionsModel",
"type": "array",
"items": {
"type": "object",
"properties": {
"section_name": { "type": "string" },
"options" : { "type" : "array",
"items" : {
"type" : "object",
"properties" : {
"awshack" : {
"type" : "array",
"items" : { "type" : "string" }
}
}
}
}
}
}
}
在映射模板中,"awshack" 被滑入最内层循环之外。
#foreach($items in $question.options.L)
{"awshack" :
[#foreach($item in $items.L)
"$item.S"#if($foreach.hasNext),#end
#end
#if($foreach.hasNext),#end
]}#if($foreach.hasNext),#end
#end
(这是我的模型方案:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "QuestionsModel",
"type": "array",
"items": {
"type": "object",
"properties": {
"section_name": { "type": "string" },
"options" : {
"type" : "array",
"items" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
}
这是映射模板:
#set($inputRoot = $input.path('$'))
[
#foreach($question in $inputRoot) {
"section_name" : "$question.section_name.S",
"options" : [
#foreach($items in $question.options.L) {
[
#foreach($item in $items.L) {
"$item.S"
}#if($foreach.hasNext),#end
#end
]
}#if($foreach.hasNext),#end
#end
]
}#if($foreach.hasNext),#end
#end
]
尽管此语法正确地映射了数据,但它导致 "options" 成为一个空数组。
如果没有指定 "options",那么我的 iOS 应用会收到有效的 JSON。但是,当我尝试 "options" 的各种语法时,我会得到无效的 JSON 或 "Internal Service Error" 并且 CloudWatch 提供的 Unable to transform response
.
有效选项使用以下内容填充:{L=[{"L":[{"S":"1"},{"S":"Dr"}]},{"L":[{"S":"2"},{"S":"Mr"}]},{"L":[{"S":"3"},{"S":"Ms"}]},{"L":[{"S":"4"},{"S":"Mrs"}]},{"L":[{"S":"5"},{"S":"Prof."}]}]}
,由 Lambda 函数提供。
此时我只能得出结论,API Gateway VTL 不支持嵌套数组。
AWS iOS SDK for Modeling 不支持数组的数组。
您必须在任何嵌套数组之间定义一个字典。 因此,而不是 array/object/array/array 你滑入一个额外的 "awshack" 对象:array/object/array/awshack-object/array
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "QuestionsModel",
"type": "array",
"items": {
"type": "object",
"properties": {
"section_name": { "type": "string" },
"options" : { "type" : "array",
"items" : {
"type" : "object",
"properties" : {
"awshack" : {
"type" : "array",
"items" : { "type" : "string" }
}
}
}
}
}
}
}
在映射模板中,"awshack" 被滑入最内层循环之外。
#foreach($items in $question.options.L)
{"awshack" :
[#foreach($item in $items.L)
"$item.S"#if($foreach.hasNext),#end
#end
#if($foreach.hasNext),#end
]}#if($foreach.hasNext),#end
#end