json.Unmarshal not working :(

agolangf · · 564 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am pulling my hear over this problem. Unmarshal is simply not working, even though I copied it verbatim from the documentation. config.json</p> <pre><code>{ &#34;testKey&#34;: &#34;test_value&#34; } </code></pre> <p>main.go</p> <pre><code>package main import ( &#34;fmt&#34; &#34;log&#34; &#34;io/ioutil&#34; &#34;encoding/json&#34; ) type Configuration struct { testKey string } func main() { // Read config file b, err := ioutil.ReadFile(&#34;config.json&#34;) // I checked b, it contains the config file contents if err != nil { log.Fatalf(&#34;Error reading config.json: %s&#34;, err) } configuration := Configuration{} err = json.Unmarshal(b, &amp;configuration) if err != nil { fmt.Println(&#34;error:&#34;, err) } fmt.Printf(&#34;%s&#34;, configuration.testKey) // Prints nothing, just empty } </code></pre> <hr/>**评论:**<br/><br/>Justinsaccount: <pre><p>yeah, that&#39;s a fun one. From the docs:</p> <p>To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match. Unmarshal will only set exported fields of the struct. </p> <p>you likely want something like</p> <pre><code>type Configuration struct { TestKey string `json:&#34;test_key&#34;` } </code></pre></pre>sjalfurstaralfur: <pre><p>Thanks, adding the <code>json</code> part made it work!</p> <p>Also... this is weird but it only works the struct fields are CapitalizedLikeThis, not capitalizedLikeThis... is this some implicit rule of golang structs that I did not know about? Seems so weird.</p></pre>skidooer: <pre><blockquote> <p>is this some implicit rule of golang structs that I did not know about?</p> </blockquote> <p>Not only structs, it is <a href="https://golang.org/ref/spec#Exported_identifiers" rel="nofollow">a general rule in Go</a>. <code>CapitalizedLikeThis</code> is exported (pubic), <code>capitalizedLikeThis</code> is unexported (private).</p> <p>The <code>json</code> package doesn&#39;t have permission, if you will, to modify <code>capitalizedLikeThis</code>.</p></pre>Justinsaccount: <pre><p>no.. capitalizing the name is what made it work.</p></pre>sjalfurstaralfur: <pre><p>Ok, I did not know about this Go characteristic, TIL. Thanks for the help</p></pre>thepciet: <pre><p>Yup this is a standard gotcha everybody has to go through, but having JSON just work is worth it.</p></pre>Jherden: <pre><p>also, for future reference, you can pick brains on the #go-nuts IRC as well. getting answers are hit or miss, but sometimes it&#39;s worth potentially getting the quick feedback.</p></pre>gogroob: <pre><p>you can only Unmarshal to an exported struct key. Try <code> Configuration struct { TestKey string } </code></p></pre>KyleBrandt: <pre><p>Note if you are working with existing JSON. You can pipe the text to go-json: <a href="https://github.com/ChimeraCoder/gojson" rel="nofollow">https://github.com/ChimeraCoder/gojson</a> , and it will generate a go struct for you, with the json struct tags. </p> <p>Huge time saver.</p></pre>

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

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