【问题标题】:how to parse response from AWS Go API如何解析来自 AWS Go API 的响应
【发布时间】:2016-05-29 19:26:54
【问题描述】:

我正在使用以下示例程序:

func getEnv(appName string, env string) {
    svc := elasticbeanstalk.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})

    params := &elasticbeanstalk.DescribeConfigurationSettingsInput{
        ApplicationName: aws.String(appName), // Required
        EnvironmentName: aws.String(env),
    }
    resp, err := svc.DescribeConfigurationSettings(params)

    if err != nil {
        fmt.Println(err.Error())
        return
    }
    v := resp.ConfigurationSettings
    fmt.Printf("%s", v)
}

它正在打印以下响应;除了缺少引号之外,这看起来像一个有效的 json。例如:ApplicationName 而不是“ApplicationName”。

如何解析这个?还是从 AWS 获取有效的 json?

ConfigurationSettings: [{
          ApplicationName: "myApp",
          DateCreated: 2016-01-12 00:10:10 +0000 UTC,
          DateUpdated: 2016-01-12 00:10:10 +0000 UTC,
          DeploymentStatus: "deployed",
          Description: "Environment created from the EB CLI using \"eb create\"",
          EnvironmentName: "stag-myApp-app-s1",
          OptionSettings: [
            ...

【问题讨论】:

  • resp.ConfigurationSettings 不需要解析,已经是[]*ConfigurationSettingsDescription了。

标签: json amazon-web-services go


【解决方案1】:

resp.ConfigurationSettings 不再是 JSON 格式,aws-sdk-go 包会为您处理。当你这样做时,

v := resp.ConfigurationSettings

v 包含一个实例 []*ConfigurationSettingsDescription,它是从 JSON 响应中解析出来的,您不必自己解析它。打印出来时看到的是 Go 结构表示。你可以继续使用它:

if len(v) > 0 {
    log.Println(v[0].ApplicationName)
}

这应该打印出myApp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2014-01-21
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多