0

我正在使用 AWS DynamoDB 和 ASW API Gateway。我有一个 API 可以从 DB 中获取一些内容,格式如下:

    {
      "Count": 1,
      "Items": [
        {
          "id": {
            "S": "111"
          },
          "title": {
            "S": "title"
          },
          "description": {
            "L": [
              {
                "S": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
              },
              {
                "S": "Lorem ipsum dolor sit amet, consectetur adipiscing elitDuis felis nibh, 
    mattis 
    vitae augue a, congue malesuada orciDuis felis nibh, mattis vitae augue a, congue malesuada 
    orci"
              },
              {
                "S": "Nullam in gravida sem. Nulla sagittis dignissim ultricies"
              }
            ]
          }
        }
      ]
    }

我需要解析为这样的东西:

    {
      "response": [
        {
          "id": "111",
          "title": "title",
          "description": [
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "Duis felis nibh, mattis vitae augue a, congue malesuada orciDuis felis nibh, mattis 
    vitae augue a, congue malesuada orci",
            "Nullam in gravida sem. Nulla sagittis dignissim ultricies"
          ]
        }
      ]
    }

这是我的响应映射模板代码:

    #set($inputRoot = $input.path('$'))
    {
        "response": [
            #foreach($elem in $inputRoot.Items) {
                "id": "$elem.id.S",
                "description": [
                   #foreach($paramName in $elem.description.L)
                     "$paramName.S" 
                     #if($foreach.hasNext),#end],
                "title": "$elem.title.S"
            }#if($foreach.hasNext),#end
        #end
        ]
    }

idtitle我可以解析,但不能解析description。请让我知道我做错了什么。

4

1 回答 1

0

您只是忘记了一个 END 语句 :-)

#set($inputRoot = $input.path('$'))
{
    "response": [
        #foreach($elem in $inputRoot.Items) {
            "id": "$elem.id.S",
            "title": "$elem.title.S",
            "description": [
               #foreach($paramName in $elem.description.L)
                "$paramName.S"#if($foreach.hasNext),#end
                #end
            ]
        }#if($foreach.hasNext),#end
    #end
    ]
}
于 2020-03-06T08:08:22.820 回答