[英]How to convert *elbv2.DescribeLoadBalancersOutput to an JSON in goLang to perform data parsing
I have been trying to work with AWS Go SDK Version 2. I am getting an where I want to convert the response I got from DescribeLoadBalancer() function into a JSON, so that I can parse it and perform further action but when I try to follow how to parse the JSON in go describe here https://blog.alexellis.io/golang-json-api-client/ .我一直在尝试使用 AWS Go 开发工具包版本 2。我想要将从DescribeLoadBalancer()函数获得的响应转换为 JSON,以便我可以解析它并执行进一步的操作,但是当我尝试遵循如何在 go describe here https://blog.alexellis.io/golang-json-api-client/中解析 JSON。 I get an error in the compiler that I can't convert * elbv2.DescribeLoadBalancersOutput into []byte .
我在编译器中收到一个错误,我无法将 * elbv2.DescribeLoadBalancersOutput转换为[]byte 。 I tried different approaches ie trying to convert * elbv2.DescribeLoadBalancersOutput into string etc but I always get the same error ie cannot convert * elbv2.DescribeLoadBalancersOutput into the specified type.
我尝试了不同的方法,即尝试将 * elbv2.DescribeLoadBalancersOutput转换为字符串等,但我总是得到相同的错误,即无法将 * elbv2.DescribeLoadBalancersOutput转换为指定的类型。 hence want to understand what would be the best to perform the required action.
因此想要了解执行所需操作的最佳方法。 Here's my code.
这是我的代码。
package main
import (
"encoding/gob"
"fmt"
"reflect"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elbv2"
)
type LoadBalancerARN struct {
loadBalancerARN string
}
func main() {
session, error := session.NewSession(&aws.Config{
Region: aws.String("eu-central-1"),
})
if error != nil {
fmt.Printf("Cannot create a session, %v", error)
}
service := elbv2.New(session)
input := &elbv2.DescribeLoadBalancersInput{}
getLoadBalancersJson, error := service.DescribeLoadBalancers(input)
if error != nil {
if aerr, ok := error.(awserr.Error); ok {
switch aerr.Code() {
case elbv2.ErrCodeLoadBalancerNotFoundException:
fmt.Println(elbv2.ErrCodeLoadBalancerNotFoundException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(error.Error())
}
return
}
encodinglbJson := gob.NewEncoder(getLoadBalancersJson)
//var arn LoadBalancerARN
fmt.Println(getLoadBalancersJson)
fmt.Println(reflect.TypeOf(getLoadBalancersJson))
}
So, I have figured the above out, best way to do the above is, AWS provide a LoadBalancer function which convert the * elbv2.DescribeLoadBalancersOutput into an object of type * [] elbv2.LoadBalancer and then you can convert that object into the byte array using json.Marshal function and from there you can perform the parsing as needed.所以,我已经想通了,最好的办法是,AWS 提供了一个 LoadBalancer 函数,它将 * elbv2.DescribeLoadBalancersOutput转换为 * [] elbv2.LoadBalancer类型的对象,然后你可以将该对象转换为字节使用json.Marshal函数的数组,您可以从那里根据需要执行解析。
Here's code这是代码
type LoadBalancerJSONStructure struct {
LoadBalancerArn string
DNSName string
CanonicalHostedZoneId string
CreatedTime string
LoadBalancerName string
Scheme string
VpcId string
Type string
IpAddressType string
}
response, error := json.Marshal(getLoadBalancers.LoadBalancers)
if error != nil {
fmt.Println("Error, %v", error)
}
fmt.Println(reflect.TypeOf(getLoadBalancers.LoadBalancers))
var lbJson LoadBalancerJSONStructure
err := json.Unmarshal(response, &lbJson)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Struct is: ", lbJson)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.