【问题标题】:How do I Change the Position of Golang Struct Values?如何更改 Golang 结构值的位置?
【发布时间】:2021-09-25 00:20:30
【问题描述】:

如何更改 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"`
}

但是我怎样才能切换键和值字段的位置呢?

【问题讨论】:

  • 你为什么会关心订单?这是您打印出来的工作。不应该反映在数据本身中。
  • 我关心字段顺序的原因是因为我试图将 json 发送到 api。出于某种原因,它使用这种奇怪的格式来指定值的确切含义。
  • 什么奇怪的格式? JSON 可能是通过 http 与 API 通信时最标准的格式,它从不关心字段的顺序。
  • 嗨,奇怪的是我的意思是我使用的 api 需要你关心字段的顺序,以指定 json 值的用途
  • 是的,我也想知道他们为什么需要这种格式。但现在我只需要使用这种格式。不管怎样,我现在就联系他们

标签: arrays json go struct


【解决方案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

【讨论】:

    【解决方案2】:

    如果您使用的是官方软件包提供的json.Marshal,则不能这样做。相反,您可以实现自己的 MarhalJSON 方法,以便决定结构字段的位置。

    【讨论】:

      【解决方案3】:

      对于“我可以让单个结构类型以不同的顺序或不同的场合序列化其字段吗?”的问题? :
      没有简单的方法可以做到这一点。

      关于结构字段:您编写的代码以给定的顺序显示了分配操作,但是一旦您的代码编译完成,就再也没有该顺序的痕迹了。

      如果你真的需要这个(旁注:我很想知道你的用例吗?),你可以创建一个自定义类型,使用它自己的 .MarshalJSON() 方法,它可以容纳一个值,指示“字段应按此顺序序列化”。


      如果您需要发送异构对象数组,请使用单独的结构类型和[]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

      【讨论】:

      • 嗨,感谢您解释为什么它不完全有效。现在我似乎更好地理解了这个问题,我需要这样的 json 的原因是将数据发送到 api(由于某种原因需要这种奇怪的格式)。另外,我将如何创建一个像这样的自定义类型?
      • 哎呀,我对你有感觉。只是为了确认:向该 API 发送 [{"key":"f","value":"f"},{"key":"f","value":"f"}][{"value":"f","key":"f"},{"value":"f","key":"f"}] 无法按预期工作?
      • 嗨,以这种方式发送数据是行不通的,因为出于某种原因,他们需要值的顺序来指定值的用途。
      • 嗨,是的,我之前尝试过,但没有成功。这也可能是他们的 api 中的错误或其他东西,不确定他们为什么需要这个。
      • 这些字段实际上是key, value,还是别的什么?例如:如果typeNPC,那么下面的字段应该是name, skills, weapon,如果是monster,它们应该是weapon, skills, name ...
      猜你喜欢
      • 2016-10-11
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多