在Go语言中指定JSON的分隔符。

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

Specify separator for JSON in Go

问题

我想在Go语言中为JSON设置自定义分隔符,类似于Python中的用法:

json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':'))

如何在Go语言中指定JSON的分隔符?

澄清

json.dumps({"key1":"value1","key2":"value2"}, separators=('__','..'))

{"key2".."value2"__"key1".."value1"}

这个需求有一些有效的用例:

http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}

我需要将这个URL设置为cookie:

tracking_data=http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}

在使用http.SetCookie()设置cookie后,Chrome Web开发者工具显示:

Date:Thu, 05 Jun 2014 11:23:46 GMT
Location:http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}
Set-Cookie:538f278cbd05ae36c6000001=538f278cbd05ae36c6000001; Path=/
Set-Cookie:cyclone-track-url=http://mens.com?&cyclone_data={VisitorId:53905341bd05ae26a9000001CampaignId:538f278cbd05ae36c6000001LandingPageId:538eac3ebd05ae15fe000001OfferId:538f097ebd05ae2a4d000001SourceId:538f0e39bd05ae2b9c000002NetworkId:} Content-Length:378
Content-Type:text/html; charset=utf-8

缺少,""

我知道base64,但我希望用户能够在不进行任何手动编码/解码的情况下更改VisitorId

服务器端应用程序从Cookie和浏览器Referer中读取并比较此URL。

找到了这个问题:
https://code.google.com/p/go/issues/detail?id=7243

英文:

I want custom separators for JSON in Go, equivalent to Python's:

json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))

How to specify separator for JSON in Go ?

Clarification:

json.dumps({"key1":"value1","key2":"value2"}, separators=('__','..'))

'{"key2".."value2"__"key1".."value1"}'

There are some valid use cases for this requirement:

http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}

I need to set this same url in cookie:

tracking_data=http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}

After setting cookie with http.SetCookie(), Chrome Web Developer tool shows:

Date:Thu, 05 Jun 2014 11:23:46 GMT
Location:http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}
Set-Cookie:538f278cbd05ae36c6000001=538f278cbd05ae36c6000001; Path=/
Set-Cookie:cyclone-track-url=http://mens.com?&cyclone_data={VisitorId:53905341bd05ae26a9000001CampaignId:538f278cbd05ae36c6000001LandingPageId:538eac3ebd05ae15fe000001OfferId:538f097ebd05ae2a4d000001SourceId:538f0e39bd05ae2b9c000002NetworkId:} Content-Length:378
Content-Type:text/html; charset=utf-8

, and "" are missing.

I am aware of base64. I want users to be able to change VisitorId without any manual encode/decode routine.

Server side app reads and compares this url from Cookie and Browser Referer.

Found this issue:
https://code.google.com/p/go/issues/detail?id=7243

答案1

得分: 2

好的,以下是翻译好的内容:

你不能直接这样做,但是如果你想要的是我猜测的去除额外不需要的空格,你可以使用json.Compact

http://golang.org/pkg/encoding/json/#Compact

它会接收你编码后的 JSON 数据并移除不必要的空格。

你也可以尝试使用json.MarshalIndent方法,它允许你控制缩进分隔符。但据我所知,你不能指定非标准的分隔符。

英文:

Well, you can't directly, but if what you want is as I'm guessing to remove extra unneeded spaces, you can use json.Compact:

http://golang.org/pkg/encoding/json/#Compact

It just takes your encoded json and removes unnecessary spaces.

You can also try playing with the json.MarshalIndent method, that lets you control indentation separators. But AFAIK, you can't specify non standard separators.

huangapple
  • 本文由 发表于 2014年6月5日 17:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/24056066.html
匿名

发表评论

匿名网友

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

确定