json: 字符串结构标签的使用无效
json: invalid use of string struct tag
我正在尝试编写简单的 POST 无服务器 Go AWS lambda 函数。
package main
import (
"fmt"
)
import (
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
// RequestBodyType is our self-made struct to process JSON request from Client
type RequestBodyType struct {
Event string `string:"event,string,omitempty"`
EventParams EventParamsType
}
// ResponseBodyType is our self-made struct to build response for Client
type ResponseBodyType struct {
Event string `string:"event,string,omitempty"`
EventParams EventParamsType
}
// Probably problematic struct?
type EventParamsType struct {
Name string `json:"name,string,omitempty"`
Age int64 `json:"age,omitempty"`
}
// Handler function Using AWS Lambda Proxy Request
func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
// RequestBody will be used to take the json response from client and build it
requestBody := RequestBodyType{
Event: "",
EventParams: EventParamsType{
Name: "",
Age: 0,
},
}
// Unmarshal the json, return 404 if error
err := json.Unmarshal([]byte(request.Body), &requestBody)
if err != nil {
return events.APIGatewayProxyResponse{Body: err.Error(), StatusCode: 404}, nil
}
// We will build the BodyResponse and send it back in json form
responseBody := &ResponseBodyType{
Event: requestBody.Event,
EventParams: EventParamsType{
Name: requestBody.EventParams.Name,
Age: requestBody.EventParams.Age,
},
}
fmt.Println("RESPONSE BODY")
fmt.Println(responseBody)
// Marshal the response into json bytes, if error return 404
response, err := json.Marshal(&responseBody)
if err != nil {
return events.APIGatewayProxyResponse{Body: err.Error(), StatusCode: 404}, nil
}
//Returning response with AWS Lambda Proxy Response
return events.APIGatewayProxyResponse{Body: string(response), StatusCode: 200}, nil
}
func main() {
lambda.Start(Handler)
}
如果我使用单个 JSON 对象键发出 curl 请求,一切正常,例如:
curl -X POST https://my.url/dev/event -d '{"event": "test"}'
然后我收到回复
{"Event":"test","EventParams":{}
但是如果我使用嵌套的 json 对象发出请求,例如:
curl -X POST https://my.url/dev/event -d '{"event": "test","eventParams": {"name": "peter","age": 13}}'
然后我得到回应
json: invalid use of ,string struct tag, trying to unmarshal "peter" into string
我相信我可能设计了 EventParamsType 错误的方式?还是我构建 ResponseBodyType 的方式有误?
如错误所述,您对 ,string
的使用对于您的 JSON 输入无效。删除它:
Name string `json:"name,omitempty"`
,string
can 在 JSON 标签中有效,它表示该数字应作为字符串文字进行封送处理。对于已经是字符串的值,这意味着它需要一个 JSON 引号字符串(您的输入显然不是)。
这个解释in the docs:
The "string" option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, integer, or boolean types. This extra level of encoding is sometimes used when communicating with JavaScript programs:
Int64String int64 `json:",string"`
有关详细信息,请参阅 in the playground。
此外,正如@Adrian 指出的那样,string:
是一个无意义的标签(无论如何,为了 JSON(未)编组的目的)。您可能需要 json:
而不是 string:
(尽管某些库可能使用名为 string:
...
的标签
我正在尝试编写简单的 POST 无服务器 Go AWS lambda 函数。
package main
import (
"fmt"
)
import (
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
// RequestBodyType is our self-made struct to process JSON request from Client
type RequestBodyType struct {
Event string `string:"event,string,omitempty"`
EventParams EventParamsType
}
// ResponseBodyType is our self-made struct to build response for Client
type ResponseBodyType struct {
Event string `string:"event,string,omitempty"`
EventParams EventParamsType
}
// Probably problematic struct?
type EventParamsType struct {
Name string `json:"name,string,omitempty"`
Age int64 `json:"age,omitempty"`
}
// Handler function Using AWS Lambda Proxy Request
func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
// RequestBody will be used to take the json response from client and build it
requestBody := RequestBodyType{
Event: "",
EventParams: EventParamsType{
Name: "",
Age: 0,
},
}
// Unmarshal the json, return 404 if error
err := json.Unmarshal([]byte(request.Body), &requestBody)
if err != nil {
return events.APIGatewayProxyResponse{Body: err.Error(), StatusCode: 404}, nil
}
// We will build the BodyResponse and send it back in json form
responseBody := &ResponseBodyType{
Event: requestBody.Event,
EventParams: EventParamsType{
Name: requestBody.EventParams.Name,
Age: requestBody.EventParams.Age,
},
}
fmt.Println("RESPONSE BODY")
fmt.Println(responseBody)
// Marshal the response into json bytes, if error return 404
response, err := json.Marshal(&responseBody)
if err != nil {
return events.APIGatewayProxyResponse{Body: err.Error(), StatusCode: 404}, nil
}
//Returning response with AWS Lambda Proxy Response
return events.APIGatewayProxyResponse{Body: string(response), StatusCode: 200}, nil
}
func main() {
lambda.Start(Handler)
}
如果我使用单个 JSON 对象键发出 curl 请求,一切正常,例如:
curl -X POST https://my.url/dev/event -d '{"event": "test"}'
然后我收到回复
{"Event":"test","EventParams":{}
但是如果我使用嵌套的 json 对象发出请求,例如:
curl -X POST https://my.url/dev/event -d '{"event": "test","eventParams": {"name": "peter","age": 13}}'
然后我得到回应
json: invalid use of ,string struct tag, trying to unmarshal "peter" into string
我相信我可能设计了 EventParamsType 错误的方式?还是我构建 ResponseBodyType 的方式有误?
如错误所述,您对 ,string
的使用对于您的 JSON 输入无效。删除它:
Name string `json:"name,omitempty"`
,string
can 在 JSON 标签中有效,它表示该数字应作为字符串文字进行封送处理。对于已经是字符串的值,这意味着它需要一个 JSON 引号字符串(您的输入显然不是)。
这个解释in the docs:
The "string" option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, integer, or boolean types. This extra level of encoding is sometimes used when communicating with JavaScript programs:
Int64String int64 `json:",string"`
有关详细信息,请参阅 in the playground。
此外,正如@Adrian 指出的那样,string:
是一个无意义的标签(无论如何,为了 JSON(未)编组的目的)。您可能需要 json:
而不是 string:
(尽管某些库可能使用名为 string:
...