Recursively reading arbitrary JSON

blov · · 554 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m having some trouble being able to unmarshal the following bit of JSON:</p> <pre><code>{ &#34;message&#34;: &#34;hello&#34;, &#34;geo_longlat&#34;: &#34;{-96.8207,32.7825}&#34; } </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&#39;m a bit lost on how to now unmarshal the geo_longlat value. I&#39;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), &amp;y) s := y[&#34;geo_longlat&#34;].(string) json.Unmarshal([]byte(s), &amp;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>&#39;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:&#34;geo_longlat&#34;</code> }</p> <p>Here&#39;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&#39;ve done it for something if you&#39;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&#39;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&#39;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&#39;s all you&#39;re going to get. b) you are seemingly trying to decode the string contained in geo_longlat as json. But it&#39;s not actual JSON, it&#39;s something else. {} in json denote objects, so they&#39;d need keys that aren&#39;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>&#34;{-96.8207,32.7825}&#34;</code> with <code>[-96.8207,32.7825]</code> because there is no reason why this can&#39;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 &#34;geoip_longlat&#34; instead of &#34;geo_longlat&#34;.</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&#39;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>&#34;{}&#34; is only for &#34;object&#34; notation&#34;.</p> <p>If you want an array, you should use &#34;[]&#34;.</p> <p>&#34;{-96.8207,32.7825}&#34; 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&#39;t change the data you can use UnmarshalJSON to define your own type</p></pre>

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

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