<p>I'm having some trouble being able to unmarshal the following bit of JSON:</p>
<pre><code>{
"message": "hello",
"geo_longlat": "{-96.8207,32.7825}"
}
</code></pre>
<p>There could be any number of other fields in the message, but the only thing I care about is the geo_longlat right now. With the below code, I'm a bit lost on how to now unmarshal the geo_longlat value. I'd appreciate any suggestions or if someone might be able to point me in the right direction. </p>
<pre><code>var y map[string]interface{}
var x []float64
json.Unmarshal([]byte(data), &y)
s := y["geo_longlat"].(string)
json.Unmarshal([]byte(s), &x)
fmt.Println(x)
</code></pre>
<p>result:</p>
<pre><code>[]
</code></pre>
<p>edit: Thanks for all the suggestions! I ultimately ended up doing a strings.Replace of {} to [] of the above JSON per <a href="/u/pierrrre">/u/pierrrre</a>'s <a href="https://www.reddit.com/r/golang/comments/406tvv/recursively_reading_arbitrary_json/cyrwmi6">comment</a></p>
<hr/>**评论:**<br/><br/>Dalton: <pre><p>You can just create a struct representation of the JSON to easily unmarshal it in. </p>
<p>Something like</p>
<p>type GeoLocation struct {
GeoLongLat string <code>json:"geo_longlat"</code>
}</p>
<p>Here's an example in the go playground:</p>
<p><a href="https://play.golang.org/p/brVaEAKNaq">https://play.golang.org/p/brVaEAKNaq</a></p></pre>quiI: <pre><p>This is the solution you want. </p>
<p>But if you actually want to traverse JSON recursively, well I've done it for something if you're curious</p>
<p><a href="https://github.com/quii/jsonequaliser" rel="nofollow">https://github.com/quii/jsonequaliser</a></p>
<p>If you could tag code as NSFL this would be it ;)</p></pre>blasstula: <pre><p>Thanks; I'm able to get geo_longlat as a string now, but still having issues getting it as a []float64 here: <a href="https://play.golang.org/p/NYP1Z-Envt" rel="nofollow">https://play.golang.org/p/NYP1Z-Envt</a></p></pre>oefig: <pre><p>The value n your <code>geo_longlat</code> isn't valid JSON that can be unmarshaled into anything but a string.</p>
<p><a href="https://play.golang.org/p/s1SWQNUqRt">https://play.golang.org/p/s1SWQNUqRt</a></p></pre>TheMerovius: <pre><p>a) the geo_longlat value is just a string for json. So that's all you're going to get. b) you are seemingly trying to decode the string contained in geo_longlat as json. But it's not actual JSON, it's something else. {} in json denote objects, so they'd need keys that aren't there. You might try something like this:
<a href="http://play.golang.org/p/S1wOCmAXdr">http://play.golang.org/p/S1wOCmAXdr</a>
i.e. rewriting it to be valid json, but that might be brittle and lead to unexpected results if you get weird values.</p>
<p>The real answer is that the people whose API you are using should make their stuff correct and replace <code>"{-96.8207,32.7825}"</code> with <code>[-96.8207,32.7825]</code> because there is no reason why this can't just be json all the way.</p></pre>UTF64: <pre><p>in your json</p>
<blockquote>
<p>geo_longlat</p>
</blockquote>
<p>in your code</p>
<blockquote>
<p>geoip_longlat</p>
</blockquote>
<p>please tell me this is not the issue</p></pre>blasstula: <pre><p>Sorry, typo. updated.</p></pre>pierrrre: <pre><p>First, there is an error in your code: you use "geoip_longlat" instead of "geo_longlat".</p>
<p>Please use the Golang Playground for your code: <a href="http://play.golang.org/" rel="nofollow">http://play.golang.org/</a></p>
<p>YOU MUST ALWAYS CHECK ERRORS!!!</p>
<p>Here is a valid code: <a href="http://play.golang.org/p/qqj4lXRHKA" rel="nofollow">http://play.golang.org/p/qqj4lXRHKA</a>
But it still doesn't work.</p></pre>pierrrre: <pre><p>Here is a working code: <a href="http://play.golang.org/p/gzNRFXNk2B" rel="nofollow">http://play.golang.org/p/gzNRFXNk2B</a></p>
<p>"{}" is only for "object" notation".</p>
<p>If you want an array, you should use "[]".</p>
<p>"{-96.8207,32.7825}" is not valid JSON.</p></pre>blasstula: <pre><p>Thanks, I did a strings.Replace on the braces and that worked. Unfortunately my data source requires it in this format for some reason, but at least I can get around it now.</p></pre>itswzee: <pre><p><a href="https://play.golang.org/p/bYC58EpFj2" rel="nofollow">https://play.golang.org/p/bYC58EpFj2</a></p>
<p>If you can't change the data you can use UnmarshalJSON to define your own type</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传