<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't seen that before. Very handy.</p></pre>DeedleFake: <pre><p>That's a neat one. Unfortunately, it doesn't seem to handle everything properly. Try this one: <code>[{"id": "test", "value": 3}, {"id": "second", "value": "This is another test."}]</code></p></pre>mwholt: <pre><p>It'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're dealing with someone else'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 "struct" 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'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>{
"name": "Resource",
"pluralName": "Resources",
"parent": "Base",
"description": "This is the parent class of all resources with some default attributes defined",
"fields": {
"healthState": {
"mandatory": true,
"description": "",
"dataType": "enumeration",
"enumValues": [ "healthy", "error", "probation", "maintenance", "unknown"],
"defaultValue" : "unknown"
},
"policyGroups" : {
"description": "The policy group associated with this resource",
"dataType": "relationship",
"relationType": "Reference",
"refDataType": "PolicyGroup",
"cardinality": "Many"
}
}
}
</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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传