如何将JSON转换为CSV?

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

How to Convert JSON to CSV?

问题

如何修复这个错误?

http://play.golang.org/p/0UMnUZOUHw

// Golang中的JSON转CSV
package main

import (
	"encoding/csv"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

type Json struct {
	RecordID int64  `json:"recordId"`
	DOJ      string `json:"Date of joining"`
	EmpID    string `json:"Employee ID"`
}

func main() {
	// 从JSON文件中读取数据
	data, err := ioutil.ReadFile("./people.json")
	if err != nil {
		fmt.Println(err)
	}
	// 反序列化JSON数据
	var d []Json
	err = json.Unmarshal([]byte(data), &d)
	if err != nil {
		fmt.Println(err)
	}
	// 创建一个csv文件
	f, _ := os.Create("./people.csv")
	defer f.Close()
	// 将反序列化的json数据写入CSV文件
	w := csv.NewWriter(f)

	// 如何继续进行?
	/* 如果我使用下面的代码,我会得到错误
	for _, obj := range d {
	  var record []interface{}
	  record = append(record, obj.RecordID)
	  record = append(record, obj.DOJ)
	  record = append(record, obj.EmpID)
	  w.Write(record)
	}
	*/
}

错误信息:

无法将record(类型为[]interface {})作为[]string类型的函数参数使用
英文:

How can I fix the error?

http://play.golang.org/p/0UMnUZOUHw

// JSON to CSV in Golang
package main

import (
	"encoding/csv"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

type Json struct {
	RecordID int64  `json:"recordId"`
	DOJ      string `json:"Date of joining"`
	EmpID    string `json:"Employee ID"`
}

func main() {
	// reading data from JSON File
	data, err := ioutil.ReadFile("./people.json")
	if err != nil {
		fmt.Println(err)
	}
	// Unmarshal JSON data
	var d []Json
	err = json.Unmarshal([]byte(data), &d)
	if err != nil {
		fmt.Println(err)
	}
	// Create a csv file
	f, _ := os.Create("./people.csv")
	defer f.Close()
	// Write Unmarshaled json data to CSV file
	w := csv.NewWriter(f)

	// How to proceed further?
	/* I am getting errors if i use below
	for _,obj := range d {
	  var record []interface{}
	  record = append(record, obj.RecordID)
	  record = append(record, obj.DOJ)
	  record = append(record, obj.EmpID)
	  w.Write(record)
	}
	*/
}

Error:

cannot use record (type []interface {}) as type []string in function argument

答案1

得分: 16

例如,

package main

import (
	"encoding/csv"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

type Json struct {
	RecordID int64  `json:"recordId"`
	DOJ      string `json:"入职日期"`
	EmpID    string `json:"员工ID"`
}

func main() {
	// 从JSON文件中读取数据
	data, err := ioutil.ReadFile("./people.json")
	if err != nil {
		fmt.Println(err)
	}
	// 反序列化JSON数据
	var d []Json
	err = json.Unmarshal([]byte(data), &d)
	if err != nil {
		fmt.Println(err)
	}
	// 创建一个CSV文件
	f, err := os.Create("./people.csv")
	if err != nil {
		fmt.Println(err)
	}
	defer f.Close()
	// 将反序列化的JSON数据写入CSV文件
	w := csv.NewWriter(f)
	for _, obj := range d {
		var record []string
		record = append(record, strconv.FormatInt(obj.RecordID, 10))
		record = append(record, obj.DOJ)
		record = append(record, obj.EmpID)
		w.Write(record)
	}
	w.Flush()
}
英文:

For example,

package main

import (
	"encoding/csv"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

type Json struct {
	RecordID int64  `json:"recordId"`
	DOJ      string `json:"Date of joining"`
	EmpID    string `json:"Employee ID"`
}

func main() {
	// reading data from JSON File
	data, err := ioutil.ReadFile("./people.json")
	if err != nil {
		fmt.Println(err)
	}
	// Unmarshal JSON data
	var d []Json
	err = json.Unmarshal([]byte(data), &d)
	if err != nil {
		fmt.Println(err)
	}
	// Create a csv file
	f, err := os.Create("./people.csv")
	if err != nil {
		fmt.Println(err)
	}
	defer f.Close()
	// Write Unmarshaled json data to CSV file
	w := csv.NewWriter(f)
	for _, obj := range d {
		var record []string
		record = append(record, strconv.FormatInt(obj.RecordID, 10))
		record = append(record, obj.DOJ)
		record = append(record, obj.EmpID)
		w.Write(record)
	}
	w.Flush()
}

答案2

得分: 1

csv Writer的Write方法不接受[]interface{}类型的参数,而是需要一个[]string类型的参数。

英文:

csv Writer's Write does not expect an []interface{} it expects a []string.

huangapple
  • 本文由 发表于 2013年5月10日 20:02:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/16481809.html
匿名

发表评论

匿名网友

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

确定