在使用Go语言正确解析JSON数据时出现错误。

huangapple go评论98阅读模式
英文:

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 (
	    &quot;encoding/json&quot;
	    &quot;fmt&quot;
    )

    // 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(`{&quot;user&quot;: {&quot;name&quot;:[&quot;jehiah, mike, semo&quot;], &quot;password&quot;: &quot;root&quot;},
	  &quot;remote_ip&quot;: &quot;127.0.0.1&quot;, &quot;dt&quot; : &quot;[20/Aug/2010:01:12:44 -0400]&quot;}`)

	     var data DataStruct
	     err := json.Unmarshal(jsonBlob, &amp;data)
	     if err != nil {
		      fmt.Println(&quot;error:&quot;, err)
	     } else {
		      fmt.Printf(&quot;%+v&quot;, 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中并不能完全表示,例如:
   * 对象,例如 `{&quot;name&quot;:&quot;john&quot;}`
   * 数组,例如 `[&quot;john&quot;, &quot;mike&quot;, &quot;sam&quot;]`

2. CSV没有一个标准。虽然有RFC-4180,但大多数CSV编码器/解码器并不遵循它,因为微软的原因。

3. 你正在使用的包编写得非常糟糕。

CSV实现之间的主要区别之一是引号处理。考虑JSON字符串:`&quot;\&quot;Hello, world!\&quot;&quot;`。

根据我们的编组器,我们可能会得到以下任何一种字符串的CSV表示:

* `&quot;&quot;&quot;Hello, world!&quot;&quot;&quot;`
* `&quot;\&quot;Hello, world!\&quot;&quot;`
* `&#39;&quot;Hello, world!&quot;&#39;`

有些编码器甚至会转义字符串中的逗号。你描述的包绕过了这个问题,直接跳过了逗号。如果你问我,这是一个非常糟糕的设计决策。

你所需要的一切都在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. `{&quot;name&quot;:&quot;john&quot;}`
     * Arrays e.g. `[&quot;john&quot;, &quot;mike&quot;, &quot;sam&quot;]`

 2. There is no CSV standard. Well, there is RFC-4180, but most CSV encoders/decoders don&#39;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: `&quot;\&quot;Hello, world!\&quot;&quot;`.

Depending on our marshaller we could end up with any of the following CSV representations for the string:

 * `&quot;&quot;&quot;Hello, world!&quot;&quot;&quot;`
 * `&quot;\&quot;Hello, world!\&quot;&quot;`
 * `&#39;&quot;Hello, world!&quot;&#39;`

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&#39;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>



huangapple
  • 本文由 发表于 2014年8月29日 18:26:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/25566411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定