Help with JSON Unmarshal

xuanbao · · 3233 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I keep getting &#34;unexpected end of JSON input&#34; error when trying to unmarshal a response. </p> <p>Basically I hit a REST API endpoint, the output result is in &#34;JSON&#34; form but it shows as a string which I then unmarshal. The code I&#39;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, &amp;result) if unmarshallError != nil { fmt.Println(unmarshallError) log.Fatal(&#34;Error here &#34;) } return result } </code></pre> <hr/>**评论:**<br/><br/>Gacnt: <pre><p>How are you passing jobs to the function. My guess is you&#39;re calling <code>res.Close</code> on whichever data you&#39;re sending to jobs. But without seeing all the code it&#39;s just a guess. </p></pre>taway6969666: <pre><p>So long story short, I&#39;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[&#34;sid&#34;].(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 &#34;ToJSON&#34; to convert &#34;a&#34; to JSON and save the value into &#34;result&#34; works.</p> <p>The last &#34;ToJSON&#34; does not work though--it returns the error above (the unexpected end of input). </p> <p>Both &#34;a&#34; and &#34;final&#34; are technically JSON but in string format when returned. I then convert them into bytes, then unmarshal the JSON which returns as a &#34;map[string]interface{}&#34;</p> <p>Suggestions?</p> <p>E: New to go so don&#39;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&#39;t running properly</p></pre>Gacnt: <pre><p>Well that&#39;s your problem then! Can&#39;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

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