Using dot notation strings to Unmarshal deeply nested json

polaris · · 789 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am working on a system where the location of data is declared in yaml files to make the location of the data in json easily addressable. This simple function was an easy way to access deeply nested pieces of data in json. You can apply this in reverse as well.</p> <p>This will only work with nested maps, but the target data can be of any type that can normally be unmarshaled.</p> <p><a href="http://play.golang.org/p/YKTvJO9xuS">http://play.golang.org/p/YKTvJO9xuS</a></p> <hr/>**评论:**<br/><br/>inconshreveable: <pre><p>You may be interested in JSON Pointer (RFC 6910): <a href="https://tools.ietf.org/html/rfc6901">https://tools.ietf.org/html/rfc6901</a></p> <p>And a tested Go implementation: <a href="https://github.com/xeipuuv/gojsonpointer">https://github.com/xeipuuv/gojsonpointer</a></p></pre>jasonrichardsmith: <pre><p>I will definitely check that out.</p></pre>storm14k: <pre><p>I&#39;ve done something like that to walk JSON with dot notation. Its been working well so far. I was also planning to allow for indexes to arrays. Don&#39;t remember if I did it or not.</p></pre>jerf: <pre><p>Y&#39;all might be interested in googling around the phrase &#34;json query&#34;, with or without go/golang added. I don&#39;t know that you&#39;ll necessarily find an off-the-shelf solution for your exact problem, but you might get some ideas, or code you can, ah, &#34;borrow&#34;.</p></pre>jasonrichardsmith: <pre><p>I am confused by your &#34;borrow&#34; in quotes, what is that trying to communicate?</p></pre>jerf: <pre><p>&#34;Lift&#34;. &#34;Appropriate&#34;. &#34;Abscond with&#34;. &#34;Steal&#34;. </p> <p>Seriously, though, honor the LICENCE, but most of them are pretty generous in the Go community.</p></pre>jasonrichardsmith: <pre><p>By the way, I still have not found what you are talking about.</p></pre>jasonrichardsmith: <pre><p>So you are saying I stole a rather simple 17 line function.</p> <p>You are going to fall back on the idea I stole something versus, the benefit of the doubt that I came up with the idea. What if I told you this was in direct relation to a project I am currently working on? What if I told you I wrote this at 3AM because I knew it would be easy and I just needed to get it out of my head.</p> <p>Do you really think this was that hard to come up with?</p></pre>jerf: <pre><blockquote> <p>So you are saying I stole a rather simple 17 line function.</p> </blockquote> <p>No. I&#39;m saying there&#39;s been a lot of work in this area already and you may be interested in leveraging some of it, much of which is generously licensed.</p> <p>Was it 3AM last night? </p></pre>jasonrichardsmith: <pre><p>Ok, sorry, I thought you were saying I stole the code. Sorry about that.</p> <p>I consider it night, until around 6AM.</p></pre>jasonrichardsmith: <pre><p>I was thinking about this as well. I came to the conclusion that this was intended to directly address data, and I didn&#39;t feel that an array index was a reliable method to directly address data.</p></pre>613style: <pre><p>I would do it this way: <a href="http://play.golang.org/p/UyTfWKGw_7" rel="nofollow">http://play.golang.org/p/UyTfWKGw_7</a></p> <p>Now, the data is unmarshalled only once no matter how many keys are accessed, we can differentiate between error types, the code is more readable (my opinion), and we have better separation of concerns in that the code accessing the data doesn&#39;t have to care about its original encoding.</p></pre>

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

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