英文:
Error in parsing JSON data properly using Go
问题
我是一个Go的新手,尝试使用来自GitHub的库来将JSON解析为CSV。https://github.com/jehiah/json2csv
但是我遇到了这个问题:https://github.com/jehiah/json2csv/issues/22,作者没有回复。
我发现如果我们将以下JSON作为json.input提供给文件:
{"user": {"name":["jehiah, mike, semo"], "password": "root"}, "remote_ip": "127.0.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}
{"user": {"name":["jeroenjanssens", "jeroen2", "jero55"], "password": "123"}, "remote_ip": "192.168.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}
{"user": {"name":"markdata", "password": ""}, "remote_ip": "76.216.210.0", "dt" : "[20/Aug/2010:01:12:45 -0400]"}
现在,如果我尝试使用以下命令:go run main.go -k user.name -i input.json -o output.json
它返回以下输出:
"[jehiah, mike, semo]"
[jeroenjanssens jeroen2 jero55]
markdata
但是如在开放的问题中所描述的,我期望的响应是:
jehiah, mike, semo
jeroenjanssens, jeroen2, jero55
markdata
我猜这是由于这一行的原因:https://github.com/jehiah/json2csv/blob/master/main.go#L110,它在读取行时无论如何都会删除逗号。
请问如何实现上述期望的输出?
谢谢。
英文:
I'm a newbie to Go and trying use a library from github to parse JSON to CSV. https://github.com/jehiah/json2csv
But i encountered this issue: https://github.com/jehiah/json2csv/issues/22 to which the author is not replying.
I realized that if we give following JSON as json.input to the file:
{"user": {"name":["jehiah, mike, semo"], "password": "root"}, "remote_ip": "127.0.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}
{"user": {"name":["jeroenjanssens", "jeroen2", "jero55"], "password": "123"}, "remote_ip": "192.168.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}
{"user": {"name":"markdata", "password": ""}, "remote_ip": "76.216.210.0", "dt" : "[20/Aug/2010:01:12:45 -0400]"}
Now if i try using it as command: go run main.go -k user.name -i input.json -o output.json
it returns following output:
"[jehiah, mike, semo]"
[jeroenjanssens jeroen2 jero55]
markdata
but as described in opend issued, i'm expecting the response as:
jehiah, mike, semo
jeroenjanssens, jeroen2, jero55
markdata
i'm guessing it is happening due to line: https://github.com/jehiah/json2csv/blob/master/main.go#L110 which is anyhow removing commas while reading the line.
Can you please advice to how to achieve above desired output?
Regards
答案1
得分: 1
使用这段代码,您将能够非常容易地将您的JSON数据读取到Go结构中:
package main
import (
"encoding/json"
"fmt"
)
// 定义User类型
type User struct {
Name []string
Password string
}
type DataStruct struct {
User User
Remote_ip string
Dt string
}
func main() {
var jsonBlob = []byte(`{"user": {"name":["jehiah, mike, semo"], "password": "root"},
"remote_ip": "127.0.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}`)
var data DataStruct
err := json.Unmarshal(jsonBlob, &data)
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Printf("%+v", data)
}
}
如果您从未使用过encoding/json
包,您应该阅读官方的Golang JSON文章。
当您正确地将数据读取到DataStruct
结构中后,您将能够使用适当的Golang CSV包对其进行序列化。
请注意,我已经将代码中的特殊字符进行了转义,以便在文本中显示。
<details>
<summary>英文:</summary>
Using this code you will be able to read your json data in a Go struct really easily:
package main
import (
"encoding/json"
"fmt"
)
// define the User type
type User struct {
Name []string
Password string
}
type DataStruct struct {
User User
Remote_ip string
Dt string
}
func main() {
var jsonBlob = []byte(`{"user": {"name":["jehiah, mike, semo"], "password": "root"},
"remote_ip": "127.0.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}`)
var data DataStruct
err := json.Unmarshal(jsonBlob, &data)
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Printf("%+v", data)
}
}
If you have never used the *encoding/json* package you should read the official [Golang json article].
When you have correctly read the data in the *DataStruct* structure, you will be able to serialize it using the proper [Golang csv package].
[Golang json article]: http://blog.golang.org/json-and-go
[Golang csv package]: http://golang.org/pkg/encoding/csv/
</details>
# 答案2
**得分**: 0
这里涉及到几个问题:
1. JSON定义的结构在CSV中并不能完全表示,例如:
* 对象,例如 `{"name":"john"}`
* 数组,例如 `["john", "mike", "sam"]`
2. CSV没有一个标准。虽然有RFC-4180,但大多数CSV编码器/解码器并不遵循它,因为微软的原因。
3. 你正在使用的包编写得非常糟糕。
CSV实现之间的主要区别之一是引号处理。考虑JSON字符串:`"\"Hello, world!\""`。
根据我们的编组器,我们可能会得到以下任何一种字符串的CSV表示:
* `"""Hello, world!"""`
* `"\"Hello, world!\""`
* `'"Hello, world!"'`
有些编码器甚至会转义字符串中的逗号。你描述的包绕过了这个问题,直接跳过了逗号。如果你问我,这是一个非常糟糕的设计决策。
你所需要的一切都在Go的标准库中:
1. [`encoding/json`][1]
2. [`encoding/csv`][2]
[1]: http://golang.org/pkg/encoding/json/
[2]: http://golang.org/pkg/encoding/csv/
<details>
<summary>英文:</summary>
There are a couple of issues at play here:
1. The structures defined by JSON are not all representable in CSV, for example:
* Objects e.g. `{"name":"john"}`
* Arrays e.g. `["john", "mike", "sam"]`
2. There is no CSV standard. Well, there is RFC-4180, but most CSV encoders/decoders don't adhere to it because Microsoft.
3. The package you are using is very poorly coded.
One of the main differences among CSV implementation is the quote-handling. Consider the JSON string: `"\"Hello, world!\""`.
Depending on our marshaller we could end up with any of the following CSV representations for the string:
* `"""Hello, world!"""`
* `"\"Hello, world!\""`
* `'"Hello, world!"'`
Some encoders will even escape the comma inside of the string. The package you describe circumvents this issue by skipping the commas altogether. Which is a very poor design decision if you ask me.
Everything you need is in Go's standard library:
1. [`encoding/json`][1]
2. [`encoding/csv`][2]
[1]: http://golang.org/pkg/encoding/json/
[2]: http://golang.org/pkg/encoding/csv/
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论