doulao1966 2018-03-04 16:08
浏览 104

如何在Golang中将非本地类型的JSON转换为从字符串转换为整数的类型?

Below is the actual struct type in aws go sdk elbv2 package. There are other parameters in this struct. I have kept only the necessary ones for simplicity.

type CreateTargetGroupInput struct {
    _ struct{} `type:"structure"`
    Name *string `type:"string" required:"true"`
    Port *int64 `min:"1" type:"integer" required:"true"`
    Protocol *string `type:"string" required:"true" enum:"ProtocolEnum"`
    VpcId *string `type:"string" required:"true"`
}  

I need to decode a JSON string with integer number like:

{ "name": "test-tg", "port": "8080", "protocol": "HTTP" ,"vpcId": "vpc-xxxxxx"}

go code:

func main() {
    s := `{ "name": "test-tg", "port": "8080", "protocol": "HTTP" ,"vpcId": "vpc-xxxxxx"}`

    targetGroupInput := &elbv2.CreateTargetGroupInput{}
    err := json.Unmarshal([]byte(s), targetGroupInput)

    if err == nil {
        fmt.Printf("%+v
", targetGroupInput)
    }
}

I'm getting the below output, (if I pass port in integer format it'll work)

{
    Name: "testTG",
    Port: 0,
    Protocol: "HTTP",
    VpcId: "vpc-0f2c8a76"
}

My question is there is no json field in the original struct, so whatever valid name I pass as json field, it is still maaping to go field. i.e. I can send Name field as

"name, Name, NamE, etc" 

in json and it still works.

I'm confused here. For other user defined struct it wont map properly if I don't provide valid json name as defined against that go field.

Also how do I convert Port to integer from string during Json Unmarshlling?

As of now I'm using the exact copy of the struct in my project and removing the string type to integer for all integer type fields (also removed all pointers from all struct fields) and receiving the json object and I'm then converting it to integer and making the object of original struct.

It takes more time for me to maintain the exact copy by doing some changes in my project folder and to do validation against all such parameters.

I have the following questions.

  1. Is there a way to convert string to integer during unmarshlling without adding json tag like (json:",string") against the integer field.
  2. Is it a good practice to maintain an exact copy of the struct (struct of elbv2 package) with few changes?
  • 写回答

2条回答 默认 最新

  • drjmrg8766 2018-03-04 17:14
    关注

    We can define any name whether lower or upper case when unmarshal the json bytes to a struct. But we cannot define it when marshaling the struct to json. As It will not convert the lower case field name It will not take the value of json since the name should be uppercase for it to become exportable. Check this code on Go playground

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧