Importing JSON into Go

blov · · 647 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m pretty new to Go, but does anyone know how to import JSON objects into Go? Like I have a text file with a list of 100 JSON objects and I&#39;m trying to use the io and ioutil packages, but I haven&#39;t found the right command for it. I need them distinguished from each other (eg. seperate objects from each other) when they are in Go.</p> <hr/>**评论:**<br/><br/>BlobWatanabe: <pre><p><a href="https://golang.org/pkg/encoding/json" rel="nofollow">https://golang.org/pkg/encoding/json</a></p></pre>paierlep: <pre><ul> <li>read file normally</li> <li>unmarshall it</li> </ul> <p>e.g. <a href="https://gist.github.com/border/775526#file-gistfile2-go-L78" rel="nofollow">https://gist.github.com/border/775526#file-gistfile2-go-L78</a> (Line 78, main function)</p></pre>TwinBladez: <pre><p>will this work for a few 100 objects as well, or just 1? If it reads it using ioutil.ReadFile, does the unmarshall identify that the objects are seperate?</p></pre>ergotayours: <pre><p>Please don&#39;t do it paierlep&#39;s way. Reading the entire file at once is a bad idea that will cause issues for very large files. Go has a very commonly-used set of interfaces, <code>io.Reader</code> and <code>io.Writer</code>, that allow you to use buffered I/O instead of reading into massive byte arrays.</p> <p>To read JSON into a Go struct, use <code>json.Decoder</code>. Let&#39;s say that you have the following JSON in a file called <code>data.json</code>:</p> <pre><code>{ &#34;name&#34;: &#34;TwinBladez&#34;, &#34;monthsActive&#34;: 2, &#34;latestLink&#34;: &#34;Importing JSON into Go&#34; } </code></pre> <p>Let&#39;s say we wanted to check if the user&#39;s name was <code>TwinBladez</code>. In Go, we&#39;d do the following:</p> <p><a href="https://play.golang.org/p/UB5Z4J4Bqr" rel="nofollow">https://play.golang.org/p/UB5Z4J4Bqr</a></p> <p>Note that this won&#39;t run properly on the Playground because it references a nonexistent file <code>data.json</code>.</p> <p>Check out the <a href="https://golang.org/pkg/encoding/json" rel="nofollow">encoding/json documentation</a>, especially the Marshal and Unmarshal documentation, for more information on how the package works.</p></pre>TwinBladez: <pre><p>This program seems to only work for 1 object. What if I had like 100 that were in the same file eg. {&#34;name&#34;:&#34;TwinBladez&#34;,&#34;monthsActive&#34;: 2,&#34;latestLink&#34;: &#34;Importing JSON into Go&#34;}{&#34;name&#34;:&#34;Reddit&#34;,&#34;monthsActive&#34;: 10,&#34;latestLink&#34;: &#34;JSON&#34;}...</p></pre>ergotayours: <pre><p>Ah, sorry, I forgot to answer that part. Whatever encoding/json reads from has to be well-formed JSON. Since JSON files must only describe a single JSON entity (usually an object <code>{}</code> or array <code>[]</code>), this file is not JSON.</p> <p>I would ask the person who&#39;s telling you this can&#39;t be an array why they can&#39;t use correct JSON. </p> <p>EDIT: <a href="/u/SaidinWoT" rel="nofollow">/u/SaidinWoT</a> is <a href="https://www.reddit.com/user/SaidinWoT" rel="nofollow">right</a>, Decoder can handle multiple JSON objects at the top level of a stream. I was looking at that very example earlier and didn&#39;t realize that the raw JSON wasn&#39;t wrapped in an array, whoops.</p></pre>TwinBladez: <pre><p>Okay, thanks for the advice! Helped me a ton. So there&#39;s no way of importing this file without making it an array with []?</p></pre>nhooyr: <pre><p>no there is, look at what SaidinWoT said.</p></pre>TwinBladez: <pre><p>I think I found what the problem is. The keys inside the JSON objects, in the form of {&#34;birthday_date&#34;:10...} where the first letter of the name is lower case. Will this really mess with the decoding? And is there a way around this?</p></pre>hayzeus: <pre><p>That is a perfectly legitimate key name. As others have said, your issue is that the file itself is malformed json. The whole thing needs to be surrounded by brackets, and each object should be separated by a comma.</p> <p>Having said that, there are ways to deal with this. Have a look at the example pointed out by <a href="/u/SaidinWoT" rel="nofollow">/u/SaidinWoT</a> below using the json Decoder.</p></pre>TwinBladez: <pre><p>Oh okay. I tried that method but it doesn&#39;t seem to work when I use the format with the lower cases, but works perfectly fine when I run his code (which use capital letters). This is what I ran:</p> <p><a href="https://play.golang.org/p/BJ01ydmDdV" rel="nofollow">https://play.golang.org/p/BJ01ydmDdV</a></p> <p>and this is the output</p> <p>: Knock knock. : Who&#39;s there? : Go fmt. : Go fmt who? : Go fmt yourself!</p></pre>earthboundkid: <pre><p>That&#39;s malformed. It should have a pair of square brackets around it to indicate it&#39;s an array of objects. </p></pre>TwinBladez: <pre><p>That&#39;s the thing. the person who told me that said it wasnt suppose to be an array, so i need to find a way to take it into my program without the square brackets, which im super confused about</p></pre>thukjeche: <pre><p>Well, what&#39;s the source of data, then? </p> <p>Or if that&#39;s how they&#39;re sending it to you, you should inform them that&#39;s not how JSON works. </p></pre>TwinBladez: <pre><p>It&#39;s a text file containing around 100 JSON objects, just in the form of {...}{...}... and I have no idea how to put that into Go.</p></pre>SaidinWoT: <pre><p>You may want to look at the <a href="https://golang.org/pkg/encoding/json/#example_Decoder" rel="nofollow">Decoder example</a> in the encoding/json package - it seems to handle exactly what you&#39;re looking for. Decoders can decode objects in sequence like that with multiple calls to Decode.</p></pre>MrCiziski: <pre><p>While this might not be valid JSON, in the real world, having to massage poorly formatted data is unfortunately a reality :).</p> <p>Try the following:</p> <p><a href="https://play.golang.org/p/sKNTHG8Ld2" rel="nofollow">https://play.golang.org/p/sKNTHG8Ld2</a></p> <p>It implements a line scanner, wraps the strings in an io.Reader, and deserializes them into an object.</p></pre>-Nii-: <pre><p>How do you handle a correctly formed JSON array with your example code above?</p></pre>earthboundkid: <pre><p><a href="https://play.golang.org/p/-fyH73Zb4V" rel="nofollow">Marshal into a slice of <code>RedditUser</code> instead of just one</a>.</p></pre>-Nii-: <pre><p>Awesome thanks!</p></pre>

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

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