如何使用Go中的排序键生成JSON? [英] How to produce JSON with sorted keys in Go?
本文介绍了如何使用Go中的排序键生成JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在python中,您可以通过按照排序顺序生成JSON来生成JSON。
import json
print json.dumps ({'4':5,'6':7},sort_keys = True,indent = 4,separators =(',',':'))
我还没有在Go中找到类似的选项。任何想法我都可以实现类似的行为吗?
解决方案
json包总是在编组时总是命令键。具体来说:
-
地图的按键按照字典顺序排序
>结构键按照struct中定义的顺序编组
实现在这里使用ATM:
In python you can produce JSON with keys in sorted order by doing
import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4, separators=(',', ': '))
I have not found a similar option in Go. Any ideas how I can achieve similar behavior in go?
解决方案
The json package always orders keys when marshalling. Specifically:
Maps have their keys sorted lexicographically
Structs keys are marshalled in the order defined in the struct
The implementation lives here ATM:
这篇关于如何使用Go中的排序键生成JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文