make a map with different data types in go for making post request with json data

huangapple go评论71阅读模式
英文:

make a map with different data types in go for making post request with json data

问题

我正在尝试在Go/Golang中创建一个键值对的映射,并在进行HTTP POST请求之前使用json.Marshal进行序列化。

jsonData的结构如下:

{
    "name":"bob",
    "stream":"science",
    "grades":[
        {
            "maths":"A+",
            "science":"A"
        }
    ]
}

这个映射的结构是这样的:它有字符串类型的键,值可以是字符串,还可以是一个切片,而切片本身又包含一个映射。所以从Python的角度来看,我想创建一个字典,其中有键值对,但最后一个键的值是一个列表,而列表中又包含一个字典。

代码中的一部分如下:

postBody, err := json.Marshal(map[string]interface{}{
    "name": name,
    "stream": stream,
    "grades": [
        {
            sub1: sub1_score,
            sub2: sub2_score
        }
    ]
})

但是我无法创建这种复杂的映射。

英文:

I am trying to create a map of key values and then json.Marshal it before making an HTTP post request in Go/Golang

jsonData

{"name":"bob",
"stream":"science",
"grades":[{"maths"    :"A+",
           "science"  :"A"}]
}

The structure of the map is like, it has string typed keys and values are strings and a slice and the slice itself has a map. So in terms of python I want to make a dictionary which has key value pairs but last key's value is a list and the list has a dictionary in it.

a part from code is this:

postBody, err := json.Marshal(map[string]interface{}{
		"name":name,
        "stream":stream,
        "grades":[{sub1  :sub1_score,
                   sub2  :sub2_score}]
	    })

but failed to make this kind of complex map.

答案1

得分: 2

postBody, err := json.Marshal(map[string]interface{}{
    "name":   name,
    "stream": stream,
    "grades": []map[string]interface{}{{
        sub1: sub1_score,
        sub2: sub2_score,
    }},
})

或者,如果你不想重新输入 map[string]interface{}

type Obj map[string]interface{}

postBody, err := json.Marshal(Obj{
    "name":   name,
    "stream": stream,
    "grades": []Obj{{
        sub1: sub1_score,
        sub2: sub2_score,
    }},
})

https://go.dev/play/p/WQMiE5gsx9w

英文:
postBody, err := json.Marshal(map[string]interface{}{
	"name":   name,
	"stream": stream,
	"grades": []map[string]interface{}{{
        sub1: sub1_score,
        sub2: sub2_score,
    }},
})

or, if you'd like to avoid having to retype map[string]interface{}

type Obj map[string]any

postBody, err := json.Marshal(Obj{
	"name":   name,
	"stream": stream,
	"grades": []Obj{{
        sub1: sub1_score,
        sub2: sub2_score,
    }},
})

https://go.dev/play/p/WQMiE5gsx9w

答案2

得分: 0

Go是一种静态类型语言。

一个空接口可以保存任何类型的值。但是你的嵌套列表没有类型。

之前

[{sub1  :sub1_score, sub2  :sub2_score}]

之后

[]map[string]interface{}{
	{
        sub1: sub1_score,
	    sub2: sub2_score,
    },
}
英文:

Go is a statically typed language.

An empty interface may hold values of any type. But your nested list has no type.

Before

[{sub1  :sub1_score, sub2  :sub2_score}]

After

[]map[string]interface{}{
	{
        sub1: sub1_score,
	    sub2: sub2_score,
    },
}

huangapple
  • 本文由 发表于 2022年4月8日 15:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/71793121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定