英文:
How do I Change the Position of Golang Struct Values?
问题
如何更改JSON值的位置?
我想要实现的目标是:
[{"key":"f","value":"f"},{"value":"f","key":"f"}]
问题:
type Struct struct {
Key string `json:"key"`
Value string `json:"value"`
}
func main() {
test := []Struct{ {Key: "test",Value: "wep"}, {Value: "wep",Key: "test"}}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
运行此代码会打印出[{"key":"test","value":"wep"},{"key":"test","value":"wep"}]
。
我还尝试过像这样做,但它只打印出空值:
type Struct struct {
Key string `json:"key"`
Value string `json:"value"`
Value2 string `json:"value"`
Key2 string `json:"key"`
}
但是,我该如何交换键和值字段的位置呢?
英文:
How would I change the position of the json values?
What Im trying to achieve:
[{"key":"f","value":"f"},{"value":"f","key":"f"}]
Problem:
type Struct struct {
Key string `json:"key"`
Value string `json:"value"`
}
func main() {
test := []Struct{ {Key: "test",Value: "wep"}, {Value: "wep",Key: "test"}}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
Running this code prints [{"key":"test","value":"wep"},{"key":"test","value":"wep"}]
I have also tried doing something like this but it just printed empty values
type Struct struct {
Key string `json:"key"`
Value string `json:"value"`
Value2 string `json:"value"`
Key2 string `json:"key"`
}
But how would I be able to switch the position of the key and value field around?
答案1
得分: 1
对于问题“我能让一个单一的结构体类型在不同的顺序或不同的场合下序列化其字段吗?”:没有简单的方法来实现这一点。
关于结构体字段:你编写的代码显示了按照给定顺序进行赋值操作,但一旦你的代码被编译,就不再有这个顺序的痕迹了。
如果你真的需要这个功能(顺便说一句:我很好奇你的使用场景是什么?),你可以创建一个自定义类型,其中包含一个值,指示“字段应按照这个顺序进行序列化”的信息。
如果你需要发送一个异构对象数组,请使用单独的结构体类型和一个[]interface{}
数组:
type Obj1 struct{
Key string `json:"name"`
Value string `json:"value"`
}
type Obj2 struct{
Value string `json:"value"`
Key string `json:"name"`
}
func main() {
test := []interface{}{ Obj1{Key: "test",Value: "wep"}, Obj2{Value: "wep",Key: "test"}}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
链接:https://play.golang.org/p/TOk28gL0eSK
英文:
To the question "can I make a single struct type serialize its fields in a different order or different occasions ?" :
there isn't a simple way to do this.
Regarding struct fields : the code you write shows the assigning operations in a given order, but once your code is compiled, there is no trace of that order anymore.
If you really need this (side note : I would be curious to know about your use case ?), you could create a custom type, with its own .MarshalJSON()
method, which would hold a value indicating "fields should be serialized in this order".
If what you need is to send an array of heterogeneous objects, use separate struct types, and an []interface{}
array :
type Obj1 struct{
Key string `json:"name"`
Value string `json:"value"`
}
type Obj2 struct{
Value string `json:"value"`
Key string `json:"name"`
}
func main() {
test := []interface{}{ Obj1{Key: "test",Value: "wep"}, Obj2{Value: "wep",Key: "test"}}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
答案2
得分: 1
如果你正在使用官方提供的json.Marshal
方法,那是不可能做到的。相反,你可以实现自己的MarshalJSON
方法,这样你就可以决定结构体字段的位置。
英文:
If you're using json.Marshal
provided by official package it's not passible to do that. In stead, you can implement your own MarhalJSON
method so that you can decide the position of your struct fields.
答案3
得分: 1
type StructA struct {
Key string `json:"key"`
Value string `json:"value"`
}
type StructB struct {
Value string `json:"value"`
Key string `json:"key"`
}
func main() {
test := []interface{}{
StructA{Key: "test", Value: "wep"},
StructB{Value: "wep", Key: "test"},
}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
https://play.golang.org/p/72TWDU1BMaL
英文:
type StructA struct {
Key string `json:"key"`
Value string `json:"value"`
}
type StructB struct {
Value string `json:"value"`
Key string `json:"key"`
}
func main() {
test := []interface{}{
StructA{Key: "test", Value: "wep"},
StructB{Value: "wep", Key: "test"},
}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论