简体   繁体   English

如何更改 Golang Struct 值的位置?

[英]How do I Change the Position of Golang Struct Values?

How would I change the position of the json values?我将如何更改 json 值的位置?

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"}]运行此代码打印[{"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?但是我如何能够切换键和值字段的位置呢?

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".如果你真的需要这个(旁注:我很想知道你的用例?),你可以创建一个自定义类型,使用它自己的.MarshalJSON()方法,它会保存一个值,指示“字段应该在这个命令”。


If what you need is to send an array of heterogeneous objects, use separate struct types, and an []interface{} array :如果您需要发送异构对象数组,请使用单独的结构类型和[]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 https://play.golang.org/p/TOk28gL0eSK

If you're using json.Marshal provided by official package it's not passible to do that.如果您使用json.Marshal提供官方的包是不是允许通过这样做。 In stead, you can implement your own MarhalJSON method so that you can decide the position of your struct fields. MarhalJSON ,您可以实现自己的MarhalJSON方法,以便您可以决定结构字段的位置。

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 https://play.golang.org/p/72TWDU1BMaL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM