使用 Golang 解析来自 AWS api 的数据

Parsing data from AWS api using Golang

我正在使用 Connect API 从特定实例获取所有联系流,并希望将它们存储在 DynamoDB 中。

type contactFlow struct {
  Arn string 
  ContactFlowType string
  Id string
  Name string
}

func HandleRequest(ctx context.Context) (string, error) {
    var contactFlowDetails []contactFlow

    mySession := session.Must(session.NewSession())

    connectSession := connect.New(mySession)
    connectInstance := &connect.ListContactFlowsInput{
        InstanceId: aws.String("INSTANCE_ID"),
    }

    connectResult, connectError := connectSession.ListContactFlows(connectInstance)

    connectResultFlow := connectResult.ContactFlowSummaryList
    connectFlowSummaryList := awsutil.Prettify(connectResultFlow)
    fmt.Println(connectFlowSummaryList)

    json.Unmarshal([]byte(connectFlowSummaryList), &contactFlowDetails)
    fmt.Println(contactFlowDetails)

我尝试使用的 API 是这样的:https://docs.aws.amazon.com/sdk-for-go/api/service/connect/#ListContactFlowsOutput

当我在 CloudWatch Logs 上打印 connectFlowSummaryList 时确实得到了结果,但是当我打印 时它总是 returns 一个空数组 [] contactFlowDetails.

编辑 1:我想我在进行解码时发现了潜在的问题。日志的结果如下所示:

[
{
Arn: "INSTANCE_ID",
ContactFlowType: "AGENT_WHISPER",
Id: "CONTACT_FLOW_ID",
Name: "Default agent whisper"
}
]

结果的键值不在双引号内,我该如何解码这样的结果?

谢谢!

您应该做的是将 connectResultFlow.ContactFlowSummaryList 编组为 json 字符串,然后再将其传递给 awsutil.Prettify(如果需要)。

您也可以完全跳过 awsutil.Prettify,以达到此目的:

connectResultFlow := connectResult.ContactFlowSummaryList
b, err := json.Marshal(connectResultFlow)
if err != nil {
  return "", err
}
json.Unmarshal(b, &contactFlowDetails)
fmt.Println(contactFlowDetails)