<p>I have a small script I'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'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'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'll probably be easy to answer. As it stands now, the best thing I can give you is "Your struct probably doesn't match the JSON you're getting back."</p></pre>Dangle76: <pre><p>Unfortunately the server is closed to the outside :\ and my admin is gone so I can't open it to do that. The response stored in res is a giant byte array. Unless I'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>[
{
"target": "carbon.agents.metricman-a.procs",
"datapoints": [
[
245,
1516310940
],
[
245,
1516311000
]
]
}
]</p></pre>jerf: <pre><p>That sample is enough to see the problem. You'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'll decode into a slice with one <code>data</code> element for you.</p>
<p>I wasn'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'd recommend setting them to <code>[][]int</code> or something as well; you'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'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'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>' 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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传