json.Unmarshal 和 "push" 到结构数组
json.Unmarshal and "push" to array of structs
我的任务是从 AWS MSK(在 AWS lambda 中)读取记录并准备某种有效负载以将其发送到 Facebook。来自 AWS MSK 的记录是 base64 编码的,但是一旦我解码它们,我就会得到 JSON 字符串。现在我不明白 json.Unmarshal(decodedParams)
是如何变成 &jsonPayload.Data
的,它是结构数组
type Payload struct {
Data Data `json:"data,required"`
}
type Data []struct{
Event string `json:"event_name,required"`
EventTime int `json:"event_time,required"`
EventSourceUrl string `json:"event_source_url,omitempty,required"`
EventActionSource string `json:"action_source,omitempty,required"`
EventId int `json:"event_id,required"`
UserData UserDataType `json:"user_data,required"`
CustomData CustomDataType `json:"custom_data,omitempty"`
}
type CustomDataType struct {
SearchString string `json:"search_string,omitempty"`
Value json.Number `json:"value,omitempty"`
Currency string `json:"currency,omitempty"`
}
type UserDataType struct {
IpAddress string `json:"client_ip_address,omitempty,required"`
UserAgent string `json:"client_user_agent,omitempty,required"`
}
// ProcessEvent function Using AWS Lambda computed event
func ProcessEvent(event events.KafkaEvent) {
jsonPayload := Payload{}
for _, mapper := range event.Records {
for _, record := range mapper {
// Base64 decode string from MSK Kafka
decodedParams, err := base64.StdEncoding.DecodeString(record.Value)
if err != nil {
log.Fatal("Error decoding fb event params: ", err)
}
// json.Unmarshal and push to Data []structs???
unmErr := json.Unmarshal(decodedParams, &jsonPayload.Data)
if unmErr != nil {
fmt.Println("UNMARSHAL ERROR")
fmt.Println(unmErr)
}
}
}
}
func main() {
lambda.Start(ProcessEvent)
}
有效负载的最终结果应该与此类似
{
"data":[
{
"event_name":"Purchase",
"event_time":1627975460,
"action_source":"email",
"user_data":{
"em":[
"7b17fb0bd173f625b58636fb796407c22b3d16fc78302d79f0fd30c2fc2fc068"
],
"ph":[
null
]
},
"custom_data":{
"currency":"USD",
"value":"142.52"
}
},
{
"event_name":"PageView",
"event_time":1627975460,
"action_source":"email"
}
]
}
目前我遇到错误
json: cannot unmarshal object into Go value of type main.Data
因为我是 GO 的新手,我想知道我是否走在正确的道路上以及如何将解码的 json 字符串推入数据 [] 结构?如果您需要任何其他信息,请告诉我,我会提供。谢谢!
event.Records
的每一次迭代都可能会覆盖您拥有的任何内容,更多的尝试将 .Data
想象成 []data
的一部分
type data struct {
Event string `json:"event_name,required"`
EventTime int `json:"event_time,required"`
EventSourceUrl string `json:"event_source_url,omitempty,required"`
EventActionSource string `json:"action_source,omitempty,required"`
EventId int `json:"event_id,required"`
UserData UserDataType `json:"user_data,required"`
CustomData CustomDataType `json:"custom_data,omitempty"`
}
type Data []data
因此您将拥有 unmarshal
的代码并推送到 Data
slice small data
结构,类似于此。
var d data
unmErr := json.Unmarshal(decodedParams, &d)
if unmErr != nil {
fmt.Println("UNMARSHAL ERROR")
fmt.Println(unmErr)
}
jsonPayload.Data = append(jsonPayload.Data, d)
我的任务是从 AWS MSK(在 AWS lambda 中)读取记录并准备某种有效负载以将其发送到 Facebook。来自 AWS MSK 的记录是 base64 编码的,但是一旦我解码它们,我就会得到 JSON 字符串。现在我不明白 json.Unmarshal(decodedParams)
是如何变成 &jsonPayload.Data
的,它是结构数组
type Payload struct {
Data Data `json:"data,required"`
}
type Data []struct{
Event string `json:"event_name,required"`
EventTime int `json:"event_time,required"`
EventSourceUrl string `json:"event_source_url,omitempty,required"`
EventActionSource string `json:"action_source,omitempty,required"`
EventId int `json:"event_id,required"`
UserData UserDataType `json:"user_data,required"`
CustomData CustomDataType `json:"custom_data,omitempty"`
}
type CustomDataType struct {
SearchString string `json:"search_string,omitempty"`
Value json.Number `json:"value,omitempty"`
Currency string `json:"currency,omitempty"`
}
type UserDataType struct {
IpAddress string `json:"client_ip_address,omitempty,required"`
UserAgent string `json:"client_user_agent,omitempty,required"`
}
// ProcessEvent function Using AWS Lambda computed event
func ProcessEvent(event events.KafkaEvent) {
jsonPayload := Payload{}
for _, mapper := range event.Records {
for _, record := range mapper {
// Base64 decode string from MSK Kafka
decodedParams, err := base64.StdEncoding.DecodeString(record.Value)
if err != nil {
log.Fatal("Error decoding fb event params: ", err)
}
// json.Unmarshal and push to Data []structs???
unmErr := json.Unmarshal(decodedParams, &jsonPayload.Data)
if unmErr != nil {
fmt.Println("UNMARSHAL ERROR")
fmt.Println(unmErr)
}
}
}
}
func main() {
lambda.Start(ProcessEvent)
}
有效负载的最终结果应该与此类似
{
"data":[
{
"event_name":"Purchase",
"event_time":1627975460,
"action_source":"email",
"user_data":{
"em":[
"7b17fb0bd173f625b58636fb796407c22b3d16fc78302d79f0fd30c2fc2fc068"
],
"ph":[
null
]
},
"custom_data":{
"currency":"USD",
"value":"142.52"
}
},
{
"event_name":"PageView",
"event_time":1627975460,
"action_source":"email"
}
]
}
目前我遇到错误
json: cannot unmarshal object into Go value of type main.Data
因为我是 GO 的新手,我想知道我是否走在正确的道路上以及如何将解码的 json 字符串推入数据 [] 结构?如果您需要任何其他信息,请告诉我,我会提供。谢谢!
event.Records
的每一次迭代都可能会覆盖您拥有的任何内容,更多的尝试将 .Data
想象成 []data
type data struct {
Event string `json:"event_name,required"`
EventTime int `json:"event_time,required"`
EventSourceUrl string `json:"event_source_url,omitempty,required"`
EventActionSource string `json:"action_source,omitempty,required"`
EventId int `json:"event_id,required"`
UserData UserDataType `json:"user_data,required"`
CustomData CustomDataType `json:"custom_data,omitempty"`
}
type Data []data
因此您将拥有 unmarshal
的代码并推送到 Data
slice small data
结构,类似于此。
var d data
unmErr := json.Unmarshal(decodedParams, &d)
if unmErr != nil {
fmt.Println("UNMARSHAL ERROR")
fmt.Println(unmErr)
}
jsonPayload.Data = append(jsonPayload.Data, d)