GDAX api: Is this even valid JSON?

agolangf · · 503 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><code> { &#34;type&#34;: &#34;subscribe&#34;, &#34;product_ids&#34;: [ &#34;ETH-USD&#34;, &#34;ETH-EUR&#34; ], &#34;channels&#34;: [ &#34;level2&#34;, &#34;heartbeat&#34;, { &#34;name&#34;: &#34;ticker&#34;, &#34;product_ids&#34;: [ &#34;ETH-BTC&#34;, &#34;ETH-GBP&#34; ] }, ] } </code> Whats going on with the channels there? jsonlint says its garbage too...</p> <p><a href="https://docs.gdax.com/?php#overview" rel="nofollow">https://docs.gdax.com/?php#overview</a></p> <p>I know this isn&#39;t strictly a go question, but I&#39;m writing the API in go and I like you guys.</p> <ul> <li>not sure why its formating like garbage</li> </ul> <hr/>**评论:**<br/><br/>gjs278: <pre><pre><code> go func() { for { var wsDialer websocket.Dialer wsConn, _, err := wsDialer.Dial(structs.GDAXExchange.Socket, nil) if err != nil { println(err.Error()) continue } if err := wsConn.WriteJSON(map[string]string{&#34;type&#34;: &#34;subscribe&#34;, &#34;product_id&#34;: &#34;BTC-USD&#34;}); err != nil { println(err.Error()) wsConn.Close() continue } if err = wsConn.WriteJSON(map[string]string{&#34;type&#34;: &#34;subscribe&#34;, &#34;product_id&#34;: &#34;ETH-USD&#34;}); err != nil { println(err.Error()) wsConn.Close() continue } if err = wsConn.WriteJSON(map[string]string{&#34;type&#34;: &#34;subscribe&#34;, &#34;product_id&#34;: &#34;LTC-USD&#34;}); err != nil { println(err.Error()) wsConn.Close() continue } message := Message{} for { if err := wsConn.ReadJSON(&amp;message); err != nil { println(err.Error()) break } if message.Type == &#34;match&#34; { switch message.ProductId { case &#34;BTC-USD&#34;: structs.GDAXExchange.Coins[&#34;BTC&#34;].Price = message.Price structs.GDAXExchange.Coins[&#34;BTC&#34;].Buy = structs.GDAXExchange.Coins[&#34;BTC&#34;].Price structs.GDAXExchange.Coins[&#34;BTC&#34;].Sell = structs.GDAXExchange.Coins[&#34;BTC&#34;].Price case &#34;ETH-USD&#34;: structs.GDAXExchange.Coins[&#34;ETH&#34;].Price = message.Price structs.GDAXExchange.Coins[&#34;ETH&#34;].Buy = structs.GDAXExchange.Coins[&#34;ETH&#34;].Price structs.GDAXExchange.Coins[&#34;ETH&#34;].Sell = structs.GDAXExchange.Coins[&#34;ETH&#34;].Price case &#34;LTC-USD&#34;: structs.GDAXExchange.Coins[&#34;LTC&#34;].Price = message.Price structs.GDAXExchange.Coins[&#34;LTC&#34;].Buy = structs.GDAXExchange.Coins[&#34;LTC&#34;].Price structs.GDAXExchange.Coins[&#34;LTC&#34;].Sell = structs.GDAXExchange.Coins[&#34;LTC&#34;].Price } } } wsConn.Close() } }() </code></pre></pre>twek: <pre><p>That&#39;s a pretty simple example thanks for that</p></pre>deapen: <pre><p>Yes, it is valid JSON: <a href="http://json.org/" rel="nofollow">http://json.org/</a>. Arrays can have any value and the values do not need to be the same type.</p></pre>Kraigius: <pre><p>This is actually not a valid json. I have highlighted the error:</p> <pre><code>{ &#34;type&#34;: &#34;subscribe&#34;, &#34;product_ids&#34;: [ &#34;ETH-USD&#34;, &#34;ETH-EUR&#34; ], &#34;channels&#34;: [ &#34;level2&#34;, &#34;heartbeat&#34;, { &#34;name&#34;: &#34;ticker&#34;, &#34;product_ids&#34;: [ &#34;ETH-BTC&#34;, &#34;ETH-GBP&#34; ] },&lt;-- Error ] } </code></pre> <p><em>edit</em>: Enhanced highlight</p></pre>twek: <pre><p>wow, will the go json package do this? I guess an array of interfaces...?</p></pre>SilentWeaponQuietWar: <pre><p>You can, but for instances like this I typically end up using something like <a href="https://github.com/buger/jsonparser" rel="nofollow">https://github.com/buger/jsonparser</a></p></pre>dilap: <pre><p>another approach (using the std json library) would be to make a type that contains pointers to possible type of entry in the array, and has a custom UnmarshalJSON method. the UnmarshalJSON method would do just enough custom introspection to figure out what kind of element it was looking at, and then unmarshall into the appropriate entry.</p></pre>twek: <pre><p>I like the look of this library I&#39;ll try it out</p></pre>

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

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