json unmarshal not mapping to a struct?

blov · · 574 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a small script I&#39;m testing to pull json information out of graphite. When I dump the http.Get.body response into a var its in bytes as it should be, and the json.Unmarshal happens properly with a pointer to my struct, but when I try to print out the data in my struct the struct has no information in it, and I&#39;m confused as to why. Any help would be appreciated.</p> <p><a href="https://pastebin.com/SxdJ0DwX" rel="nofollow">Code</a></p> <hr/>**评论:**<br/><br/>jerf: <pre><p>To get useful help, you&#39;ll need to put the JSON response into the code as well. I suggest using the Go playground to post the request; it makes it very easy to support people in situations like this. If you include it in the post and decode from the []byte, it&#39;ll probably be easy to answer. As it stands now, the best thing I can give you is &#34;Your struct probably doesn&#39;t match the JSON you&#39;re getting back.&#34;</p></pre>Dangle76: <pre><p>Unfortunately the server is closed to the outside :\ and my admin is gone so I can&#39;t open it to do that. The response stored in res is a giant byte array. Unless I&#39;m missing something, I thought the bytes fed to json.Unmarshal were decoded when stored in the pointer to the struct?</p> <p>this is a sample response via curl however:</p> <p>[ { &#34;target&#34;: &#34;carbon.agents.metricman-a.procs&#34;, &#34;datapoints&#34;: [ [ 245, 1516310940 ], [ 245, 1516311000 ] ] } ]</p></pre>jerf: <pre><p>That sample is enough to see the problem. You&#39;re trying to decode an object, but what you have is an array. I believe if you change stuff to be <code>[]data{}</code> instead, it&#39;ll decode into a slice with one <code>data</code> element for you.</p> <p>I wasn&#39;t asking you to open the endpoint; I was asking for a sample []byte embedded in a go playground instance so we could see a sample of the source data, which is what turned out to be necessary to identify the problem, since the code looked correct.</p> <p>If you know your datapoints are ints, I&#39;d recommend setting them to <code>[][]int</code> or something as well; you&#39;re going to get tired of the casting from <code>interface{}</code>.</p></pre>Dangle76: <pre><p>Possibly, at times they can be null so setting it as a slice of int could throw an error couldn&#39;t it? I used some null examples when I used the json to go converter tool</p> <p>Changing it a slice of the struct worked perfectly. I didn&#39;t even think about the array brackets around the json object thanks!</p></pre>vagmi: <pre><p><code>json.Unmarshal</code> returns an error. You might want to check that. I think the <code>[][]interface{}</code> might be the issue. I would recommend that you unmarshal it as a <code>[]interface{}</code>. Subsequently, you can access each of these <code>Datapoints</code>&#39; value perform a type assertion on concrete array of some type.</p></pre>ChurroLoco: <pre><p>You should catch and check the error returned. </p> <p>Also it looks like the JSON is an array of objects that match you ‘data’ data type, but you are trying to marshal it into a single object. Change your local ‘stuff’ variable to be of type []data. </p> <p>var stuff []data or stuff := []data{}</p></pre>

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

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