Best way of achieving this JSON transformation?

xuanbao · · 425 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://gist.github.com/arehmandev/87a63bf5964ce4a8833294fb831c6373">Link here!</a></p> <p>Hi I&#39;ve managed to do this in hacky ways (done in Groovy, some horrendous looking code) - what would be the best way of achieving this transformation?</p> <p>Edit: Added a solution contributed by <a href="/u/titpetric">u/titpetric</a> - big thanks</p> <hr/>**评论:**<br/><br/>mm_5687: <pre><p>For those like me who struggled to spot the difference: <a href="https://i.imgur.com/BaFPwPO.png" rel="nofollow">https://i.imgur.com/BaFPwPO.png</a></p></pre>titpetric: <pre><p>Is the order of the items important? If yes, you&#39;ll have to loop through the outer slice of <code>[]Container</code> and match individual container paths to merge everything together.</p> <p>If the order isn&#39;t important, you can create a <code>map[string]Container</code> where you would use container.Path as the key, and append components to it when a key would exist.</p> <p>If you want to do this efficiently, you&#39;ll have to avoid reallocations. A way to do this would be to loop once over the containers to produce a map[string]int (key=container.path, value=count(containers with same path)). You can use <code>make(map[string]int, len(containers))</code> to produce one allocation, which allocates to your worst case scenario.</p> <p>Everything comes down to what you understand about <code>best</code>. If you want the fastest, then the best way to measure that would be to <a href="https://scene-si.org/2017/06/06/benchmarking-go-programs/" rel="nofollow">create some benchmarks</a>.</p> <p>edit: p.s. write a test in <a href="https://play.golang.org" rel="nofollow">go playground</a> as it makes it easier for other people to improve it. I would gladly rewrite a functional example, but I don&#39;t want to write the whole thing.</p></pre>absdevops: <pre><p>Big thanks.. a map was what I was missing! - I&#39;ve gotten <a href="https://play.golang.org/p/4ucN97nnvp" rel="nofollow">here so far!</a></p></pre>titpetric: <pre><p><a href="https://play.golang.org/p/B6GhzQ_JmE" rel="nofollow">https://play.golang.org/p/B6GhzQ_JmE</a> - I eliminated a bunch of excess code. The result is printed out. If you want a slice instead of a map at the end, you&#39;ll just need to range over it once more. You can create it efficiently with <code>s := make([]Containers, len(m))</code> and then append() each without additional allocations.</p> <p>Edit: here&#39;s a slice result as well: <a href="https://play.golang.org/p/St6-BOxSY6" rel="nofollow">https://play.golang.org/p/St6-BOxSY6</a> as I&#39;m in the mood :)</p></pre>absdevops: <pre><p>Ah, reading it now it makes so much sense, yet I was having a brain block looking at it earlier. I&#39;m still learning so big thanks!</p></pre>diegobernardes: <pre><p>This is pretty easy with Go: <a href="https://golang.org/pkg/encoding/json/#example__customMarshalJSON" rel="nofollow">https://golang.org/pkg/encoding/json/#example__customMarshalJSON</a></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

425 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传