<p>Hi I posted This under stackoverflow as well but thought id see if anyone here knew the answer as well.
<a href="https://stackoverflow.com/questions/48411657/golang-consume-json-with-dynamic-relevant-keys" rel="nofollow">https://stackoverflow.com/questions/48411657/golang-consume-json-with-dynamic-relevant-keys</a></p>
<p>0
down vote
favorite
I am new to GO and I am trying to consume json data from a variety of API's using GO and place them in struct's one of them Formats the data like so</p>
<pre><code>{"MS": {
"last":"25",
"highestBid":"20"},
"GE": {
"last": "24",
"highestBid": "22"}
}
</code></pre>
<p>While I can find information on Consuming with dynamic keys, all the examples I found throw away the Outer most key as arbitrary and irrelevant. I need to use it as a key value pair like bellow:</p>
<pre><code>{"MarketName": "GE", "last":"24","higestBid":"22"}
</code></pre>
<p>I understand Using Interface map but I cant figure out how to map the dynamic key to the struct as a key : value pair like above. My Code example to map leaving out the need key can be found at play ground <a href="https://play.golang.org/p/GP-8yD0knku" rel="nofollow">https://play.golang.org/p/GP-8yD0knku</a></p>
<p>as it stands it outputs</p>
<pre><code>map[MS:map[last:25 highestBid:20] GE:map[highestBid:22 last:24]]
</code></pre>
<p>As I stated I am new to GO and as much help and explanation that i can get would be very appreciated</p>
<p>**Edited to further explain the goal
I need the current dynamic key's eg. "GE" "MS" to become values in my struct eg :
{ {"MarketName": "GE", "last":"24","higestBid":"22"}, {"MarketName": "MS", "last":"25","higestBid":"20"} }</p>
<p>**edited again to show the answer given on Stackoverflow </p>
<p><a href="https://play.golang.org/p/qAp336Do5wg" rel="nofollow">https://play.golang.org/p/qAp336Do5wg</a></p>
<hr/>**评论:**<br/><br/>wastedzombie219: <pre><p>I think you want a map <a href="https://play.golang.org/p/eJLiNUqDJsg" rel="nofollow">https://play.golang.org/p/eJLiNUqDJsg</a></p></pre>t0dbld: <pre><p>No, and i have already played with that alternative and ended up with the same output as you have there. As you can see it does not match the desired output as shown in the OP. Thanks but I do have to be specific so that it can fall in line with the other 20+ sources I will have coming in</p></pre>keipra: <pre><p>What am I missing here. Everything in your sample input is in your output.</p></pre>t0dbld: <pre><p>That i need the "GE" "MS" and all the other relevant tickers to be the value of a key value pair in the struct like {"MarketName" : "GE" } show in the example in post... the name of the Ticker is obviously important and it is also not feasible to use map and have to do
x.["GE"].LAST to retrieve it.
New markets will be created all the time with out me knowing, and the purpose is to consolidate and organize the data from all the sources, I need to be able to store and use this data like i can the other 20 sources I am pulling from :-) </p></pre>GoHomeGrandmaUrHigh: <pre><p>So I tweaked your original Go snippet: <a href="https://play.golang.org/p/xQFIcQU7ume" rel="nofollow">https://play.golang.org/p/xQFIcQU7ume</a></p>
<ul>
<li>Added a <code>MarketName</code> attribute on the <code>Market</code> type. This doesn't get un-marshalled from the JSON so it gets a default blank string zero-value.</li>
<li>Made the map into <code>f := map[string]*Market</code> so that the Market structs could be modified after the fact.</li>
<li>After JSON unmarshalling, I loop over the key/value pairs in <code>f</code> and assign <code>Market.MarketName = key</code> so I copy those key names <em>into</em> the struct.</li>
</ul>
<p>In the end I have an object that I can do this to:</p>
<pre><code>// Pretty print them or whatever
for _, market := range f {
fmt.Printf("MarketName: %s Last: %s HighestBid: %s\n",
market.MarketName,
market.Last,
market.HighestBid,
)
}
Output:
MarketName: MS Last: 25 HighestBid: 20
MarketName: GE Last: 24 HighestBid: 22
</code></pre></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传