如何在将 JSON 反序列化为空接口时定义顺序?

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

How to define order while Unmarshalling json into Empty interface?

问题

我从一个API得到了JSON响应。当我尝试将其解组为一个空接口(var sample interface{})时,顺序变得随机,不按照查询的顺序。有没有办法保持与响应相同的顺序?

英文:

I have JSON response from an API. When i try to Unmarshal it into an Empty interface (var sample interface{} )the order becomes random and not according to the Query. Is there any way to maintain the same
order as the response?

答案1

得分: 4

不。

请注意,JSON对象(即{...})在Go中是无序的,所以原问题并没有太多意义,因为没有顺序需要保留。

另外,请注意,JSON数组(即[...])是有序的,当解组为切片时,它们的顺序将被保留。

英文:

No.

Note that JSON objects (the {...} things) are unordered as are maps in Go, so the question as is doesn't make much sense as there is no order to preserve.

Also note that JSON arrays (the [...] things) are orderer and their order will be preserved when unmarshaling to a slice.

huangapple
  • 本文由 发表于 2022年9月6日 17:52:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/73620032.html
匿名

发表评论

匿名网友

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

确定