-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to keep order of keys when convert map[string]interface{} to json ? #2536
Comments
This is just an example, but actual data is more complex than this. I found some third-party packages to solve this problem,but I don't know how to integrate them into goframe "gracely". |
@kkqy I couldn't reproduce the issue you described. Which specific response function did you use? |
I'm sorry, I made a mistake. The network panel of Chrome sorted the keys automatically.
Acctually,the original issue is that keys of map[string]string are sorted by alphabet. |
Slice can keep order of it's elements.
which can replace map[string]interface{} in order to keep order of elements? |
go的map输出本身就是无序的.如果key有排序规则 可以先提取所有key做排序然后再根据排序结果append到slice中.要保持原有属性json得是数组格式 |
Go's map output itself is unordered. If the key has a sorting rule, you can first extract all the keys for sorting and then append to the slice according to the sorting result. To keep the original attribute json must be an array format |
是的,golang中map的key是不会保证顺序的,但python和php之类的语言的key又是有顺序的。
代替
这样的话,我只需要修改我这个组件的struct,就能兼容其它组件传过来的有序的key的json对象了。 |
Yes, the keys of the map in golang do not guarantee the order, but the keys of languages such as python and php are in order.
replace
In this case, I only need to modify the struct of my component to be compatible with the ordered key json objects passed from other components. |
我尝试过
但是意料之中输出是[ ]
结果依旧是空[ ],也就是说gjson无法把json对象{ } 解析为切片。 所以我希望gjson提供一个特殊的类型 KeyValuePair |
依赖map/object的顺序本身就是不符合编程规范的(有顺序的只能是数组/切片,三方有序map输出基本都是基于内部的keyslice 例如gmap.NewListMap). 这个需求也仅限于你这个个例 基本不会有这方面的实现.如果有强依赖顺序应当声明key的排序方式而非依赖json编码.如果说你需要实现这个需求建议用正则去匹配json中的key自行实现. |
Relying on the order of map/object itself does not conform to the programming specification (ordered ones can only be arrays/slices, and the three-way ordered map output is basically based on internal keyslice such as gmap.NewListMap). This requirement is also limited to you There is basically no such implementation in the example. If there is a strong dependence on the order, you should declare the sorting method of the key instead of relying on the json encoding. If you need to achieve this requirement, it is recommended to use regular expressions to match the keys in json to achieve it yourself. |
有第三方包解决我的问题,但正如GoFrame文档组件统一化里说的,不想用多个包解决一个问题。于是我只能放弃gjson,完全使用第三方包。 |
There are third-party packages to solve my problem, but as stated in GoFrame document component unification, I don’t want to use multiple packages to solve one problem. So I can only give up gjson and use third-party packages completely. |
您提供的这个正则也有一点问题,就是如果遇到嵌套的json就不行了,所以解析json还是个麻烦活 |
The regex you provided also has a little problem, that is, if you encounter nested json, it will not work, so parsing json is still a troublesome job |
我部分参考别人的代码,用泛型写了一个KeyValuePairs类型,留给后来同样需要在golang里保持json的key的顺序的人。 https://github.com/kkqy/gokvpairs 用法: 下载gokvpairs包go get github.com/kkqy/gokvpairs 实例代码
输出结果:
|
1. What version of
Go
and system type/arch are you using?go version go1.20.2 windows/amd64
2. What version of
GoFrame
are you using?v2.3.3
3. Can this issue be re-produced with the latest release?
yes
4. What did you do?
INPUT is:
{"b":"b","a":"a"}
OUTPUT is:
{"a":"a","b":"b"}
5. What did you expect to see?
Keep the order of keys as that in map.
I want get {"c":"c","b":"b","a":"a"} instead of {"a":"a","b":"b","c":"c"}.
6. What did you see instead?
Keys are order by alphabet.
The text was updated successfully, but these errors were encountered: