英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论