<p>I'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'm trying to use the io and ioutil packages, but I haven'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't do it paierlep'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's say that you have the following JSON in a file called <code>data.json</code>:</p>
<pre><code>{
"name": "TwinBladez",
"monthsActive": 2,
"latestLink": "Importing JSON into Go"
}
</code></pre>
<p>Let's say we wanted to check if the user's name was <code>TwinBladez</code>. In Go, we'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'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. {"name":"TwinBladez","monthsActive": 2,"latestLink": "Importing JSON into Go"}{"name":"Reddit","monthsActive": 10,"latestLink": "JSON"}...</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's telling you this can't be an array why they can'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't realize that the raw JSON wasn't wrapped in an array, whoops.</p></pre>TwinBladez: <pre><p>Okay, thanks for the advice! Helped me a ton. So there'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 {"birthday_date":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'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's there?
: Go fmt.
: Go fmt who?
: Go fmt yourself!</p></pre>earthboundkid: <pre><p>That's malformed. It should have a pair of square brackets around it to indicate it's an array of objects. </p></pre>TwinBladez: <pre><p>That'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's the source of data, then? </p>
<p>Or if that's how they're sending it to you, you should inform them that's not how JSON works. </p></pre>TwinBladez: <pre><p>It'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'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传