<p>I keep getting "unexpected end of JSON input" error when trying to unmarshal a response. </p>
<p>Basically I hit a REST API endpoint, the output result is in "JSON" form but it shows as a string which I then unmarshal. The code I'm using to unmarshal is below. Any suggestions?</p>
<pre><code>func ToJSON(jobs string) map[string]interface{} {
bytes := []byte(jobs)
var result map[string]interface{}
unmarshallError := json.Unmarshal(bytes, &result)
if unmarshallError != nil {
fmt.Println(unmarshallError)
log.Fatal("Error here ")
}
return result
}
</code></pre>
<hr/>**评论:**<br/><br/>Gacnt: <pre><p>How are you passing jobs to the function. My guess is you're calling <code>res.Close</code> on whichever data you're sending to jobs. But without seeing all the code it's just a guess. </p></pre>taway6969666: <pre><p>So long story short, I'm trying to build a minature SDK for Splunk using the REST API endpoints and Golang.</p>
<p>There are 2 specific endpoints I am focused on. The return values for these endpoints is a JSON like string (e.g its a string but looks like JSON format).</p>
<p>I will then do something like this:</p>
<pre><code>func main() {
a, err: = Splunk.EndPointOne(param1,param2,param3)
if err != nil { fmt.Println(err.Error())}
result := ToJSON(a)
searchID := result["sid"].(string)
final, errs := Splunk.EndPointTwo(searchID)
if errs != nil { fmt.Println(errs.Error()) }
fmt.Println(ToJSON(final))
}
</code></pre>
<p>In the code sample above, the first "ToJSON" to convert "a" to JSON and save the value into "result" works.</p>
<p>The last "ToJSON" does not work though--it returns the error above (the unexpected end of input). </p>
<p>Both "a" and "final" are technically JSON but in string format when returned. I then convert them into bytes, then unmarshal the JSON which returns as a "map[string]interface{}"</p>
<p>Suggestions?</p>
<p>E: New to go so don't cruificy me for any bad standards/practices--trying to use this project as a way to learn Go :P</p></pre>Gacnt: <pre><p>What does final print if you log it?</p>
<p>And all good on being new to it. But I would suggest using an editor that uses the gofmt tool to format your code to Go standards. One nice thing about the gofmt tool is it makes reading other code very consistent </p></pre>taway6969666: <pre><p>It prints as an empty line in the terminal. It seems like this function isn't running properly</p></pre>Gacnt: <pre><p>Well that's your problem then! Can't unmarshal nothing </p></pre>taway6969666: <pre><p>Yeah I also found out over on the Splunk forums that my first method is good but I need to add an intermediary before running the second method/endpoint. Apparently there needs to be a check to see if the first method finished on the remote server before I can run the 2nd.</p>
<p>Thanks!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传