如何解析AWS Go API的响应

时间:2016-02-17 21:06:56

标签: json amazon-web-services go

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

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,除了缺少的引用make。例如: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: [
            ...

1 个答案:

答案 0 :(得分:2)

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

v := resp.ConfigurationSettings

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

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

这应打印出myApp