Jason to GO Struct parser

agolangf · · 940 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am looking for code that can parse json metadata to create Go structs which can in turn be used to extract data from the network. Anyone have links to one?</p> <hr/>**评论:**<br/><br/>jreid42: <pre><p><a href="http://golang.org/pkg/encoding/json/">http://golang.org/pkg/encoding/json/</a></p> <p><a href="https://gobyexample.com/json">https://gobyexample.com/json</a></p></pre>mwholt: <pre><p><a href="https://mholt.github.io/json-to-go/">https://mholt.github.io/json-to-go/</a> works for most things.</p></pre>jbuberel: <pre><p>Nice! Hadn&#39;t seen that before. Very handy.</p></pre>DeedleFake: <pre><p>That&#39;s a neat one. Unfortunately, it doesn&#39;t seem to handle everything properly. Try this one: <code>[{&#34;id&#34;: &#34;test&#34;, &#34;value&#34;: 3}, {&#34;id&#34;: &#34;second&#34;, &#34;value&#34;: &#34;This is another test.&#34;}]</code></p></pre>mwholt: <pre><p>It&#39;s a <a href="https://github.com/mholt/json-to-go/issues/6" rel="nofollow">known issue</a> when an array does not contain consistently-typed values.</p> <p>Pull requests welcome.</p></pre>itsmontoya: <pre><p>If your value types are changing for the same keys, I would re-evaluate the values you are storing. </p></pre>DeedleFake: <pre><p>You may not have a choice. What if you&#39;re dealing with someone else&#39;s API?</p> <p>Either way, though, <code>encoding/json</code> can handle <a href="https://play.golang.org/p/GOv_08OMAJ" rel="nofollow">this particular case</a> just fine.</p> <p><sup>Writing code on a tablet is annoying...</sup></p></pre>robotvert: <pre><p>Looking for &#34;struct&#34; in this page <a href="http://libs.club/golang/data-formats/json" rel="nofollow">http://libs.club/golang/data-formats/json</a> yielded the following projects that might interest you:</p> <p><a href="https://github.com/ChimeraCoder/gojson" rel="nofollow">https://github.com/ChimeraCoder/gojson</a></p> <p><a href="https://github.com/bashtian/jsonutils" rel="nofollow">https://github.com/bashtian/jsonutils</a></p> <p><a href="https://github.com/modcloth-labs/json2go" rel="nofollow">https://github.com/modcloth-labs/json2go</a> (seems abandoned)</p> <p>Disclaimer: I&#39;m the author of that website.</p></pre>dharanimo: <pre><p>The jsontuils seems a good starting point. I might have to do changes to what is there. Here is my problem. We express metadata of our data model in json. Here is a simple example:</p> <pre><code>{ &#34;name&#34;: &#34;Resource&#34;, &#34;pluralName&#34;: &#34;Resources&#34;, &#34;parent&#34;: &#34;Base&#34;, &#34;description&#34;: &#34;This is the parent class of all resources with some default attributes defined&#34;, &#34;fields&#34;: { &#34;healthState&#34;: { &#34;mandatory&#34;: true, &#34;description&#34;: &#34;&#34;, &#34;dataType&#34;: &#34;enumeration&#34;, &#34;enumValues&#34;: [ &#34;healthy&#34;, &#34;error&#34;, &#34;probation&#34;, &#34;maintenance&#34;, &#34;unknown&#34;], &#34;defaultValue&#34; : &#34;unknown&#34; }, &#34;policyGroups&#34; : { &#34;description&#34;: &#34;The policy group associated with this resource&#34;, &#34;dataType&#34;: &#34;relationship&#34;, &#34;relationType&#34;: &#34;Reference&#34;, &#34;refDataType&#34;: &#34;PolicyGroup&#34;, &#34;cardinality&#34;: &#34;Many&#34; } } } </code></pre> <p>I would like the mapping to be</p> <pre><code>// Comments from description type Resource struct { Base // Comments from description healthState string //My comment: All references become strings which has the id of the object policyGroups [] string } </code></pre> <p>The above mapping would be super useful for me to extract instances of resource coming on the wire from http calls. Does my problem makes sense? Is there a better way to do it?</p></pre>tclineks: <pre><p>$ go get <a href="https://github.com/tmc/json-to-struct" rel="nofollow">github.com/tmc/json-to-struct</a></p></pre>omginternets: <pre><p>I hate to be that guy, but <a href="http://lmgtfy.com/?q=json+to+go+struct+parser" rel="nofollow">come on</a>...</p></pre>dharanimo: <pre><p>Hmm.. I asked for metadata parsers, not a json parser. Please see below</p></pre>omginternets: <pre><p>Apologies! I should have read twice instead of succumbing to support-fatigue. :/</p></pre>crazyprivate: <pre><p><a href="http://json2struct.mervine.net/" rel="nofollow">Convert JSON to Struct</a></p></pre>

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

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