Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go json:字符串结构标记的使用无效_Go_Struct_Aws Lambda_Aws Lambda Go - Fatal编程技术网

Go json:字符串结构标记的使用无效

Go json:字符串结构标记的使用无效,go,struct,aws-lambda,aws-lambda-go,Go,Struct,Aws Lambda,Aws Lambda Go,我正在尝试编写简单的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 ou

我正在尝试编写简单的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"}'
curl -X POST https://my.url/dev/event  -d '{"event": "test","eventParams": {"name": "peter","age": 13}}'
我得到了回应

{"Event":"test","EventParams":{}
json: invalid use of ,string struct tag, trying to unmarshal "peter" into string
但如果我使用嵌套的json对象发出请求,例如:

curl -X POST https://my.url/dev/event  -d '{"event": "test"}'
curl -X POST https://my.url/dev/event  -d '{"event": "test","eventParams": {"name": "peter","age": 13}}'
然后我得到回应

{"Event":"test","EventParams":{}
json: invalid use of ,string struct tag, trying to unmarshal "peter" into string

我想我可能设计错了?还是我构建ResponseByType的方式不对?

正如错误所说,您使用的
、字符串对JSON输入无效。删除它:

Name字符串`json:“名称,省略为空”`
,string
可以在JSON标记中有效,并且它指示应将数字封送为字符串文本。对于已经是字符串的值,这意味着它需要一个带JSON引号的字符串(您的输入显然不是)

解释如下:

“string”选项表示字段作为JSON存储在JSON编码的字符串中。它仅适用于字符串、浮点、整数或布尔类型的字段。在与JavaScript程序通信时,有时会使用这种额外的编码级别:

Int64String int64 `json:",string"`
有关更多详细信息,请参阅



此外,正如@Adrian所指出的,
string:
是一个无意义的标记(无论如何,对于JSON(un)封送来说)。您可能需要
json:
而不是
string:
(尽管有些库可能会使用名为
string:
string:“事件,字符串,省略空”
应该是
json:“事件,字符串,省略空”
。如果结构字段类型已经是
string
,您也不需要指定
string
。您说得对!我现在从AWS得到了响应。感谢您提供的所有信息。