英文:
Return Json From lambda Go in ApiGateway
问题
我正在尝试使这个代码工作,我想要的是当从API网关调用Lambda时,从Lambda得到一个嵌套的JSON作为输出。
这是我期望的结果:
{
"data": {"username":"Random User","Age":20},
"message": "This is sample"
}
但是我得到的是转义后的JSON:
{
"data": "\n\t{\n\t\t\"username\":\"Random User\",\n\t\t\"Age\":20\n\t}",
"message": "This is sample"
}
我正在尝试的示例代码如下:
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
type Response struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
type RequestBody struct {
Data any `json:"data"`
Message string `json:"message"`
}
const (
data string = `
{
"username":"Random User",
"Age":20
}`
)
func EventHandler(request events.APIGatewayProxyRequest) (Response, error) {
response := Response{}
log.Println(request.Headers)
body := RequestBody{Data: data, Message: "This is sample"}
b, _ := json.Marshal(body)
response.Body = string(b)
response.StatusCode = http.StatusOK
log.Println("Before Return")
return response, nil
}
func main() {
lambda.Start(EventHandler)
}
请注意,我已经将代码中的双引号从"替换为了正常的双引号"。
英文:
Im trying to make this work, what i want is to get json as output from lambda when called from API gateway with some nested json.
This is what i expect,
{
"data": {"username":"Random User","Age":20}",
"message": "This is sample"
}
but im getting escaped json
{
"data": "\n\t{\n\t\t\"username\":\"Random User\",\n\t\t\"Age\":20\n\t}",
"message": "This is sample"
}
the sample code which im trying
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
type Response struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
type RequestBody struct {
Data any `json:"data"`
Message string `json:"message"`
}
const (
data string = `
{
"username":"Random User",
"Age":20
}`
)
func EventHandler(request events.APIGatewayProxyRequest) (Response, error) {
response := Response{}
log.Println(request.Headers)
body := RequestBody{Data: data, Message: "This is sample"}
b, _ := json.Marshal(body)
response.Body = string(b)
response.StatusCode = http.StatusOK
log.Println("Before Return")
return response, nil
}
func main() {
lambda.Start(EventHandler)
}
答案1
得分: 1
应用程序将data
中的JSON编码为JSON。使用json.RawMessage来消除额外的JSON编码层级。
var data = json.RawMessage(`
{
"username":"Random User",
"Age":20
}`)
英文:
The application encodes the JSON in data
to JSON. Use json.RawMessage to eliminate the extra level of JSON encoding.
var data = json.RawMessage(`
{
"username":"Random User",
"Age":20
}`)
答案2
得分: 1
这是因为要对已经包含JSON对象的字符串进行编组(编码为JSON)。相反,可以尝试以下方法:
var data = map[string]interface{}{
"username": "Random User",
"Age": 20
}
或者也可以这样做:
body := RequestBody{Data: json.RawMessage(data), Message: "This is sample"}
英文:
This is because to marshaled (encoded as JSON) a string that already contains a JSON object. Instead, try this:
var data = map[string]any {
"username":"Random User",
"Age":20
}
Or you can also do:
body := RequestBody{Data: json.RawMessage(data), Message: "This is sample"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论