希望使用Golang将json文件转换为csv文件

时间:2017-10-13 21:55:22

标签: json go

我正在尝试获取“carrier.json”文件中的数据,并使用Go和信息创建一个CSV文件。但是,我收到了以下错误/通知

./carrier_json.go:53:48: obj.APIVersion undefined (type Json has no field or method APIVersion)
./carrier_json.go:53:69: obj.CarrierSid undefined (type Json has no field or method CarrierSid)
./carrier_json.go:53:85: obj.AccountSid undefined (type Json has no field or method AccountSid)
./carrierlookup.go:12:6: main redeclared in this block
    previous declaration at ./carrier_json.go:31:6

我在网上搜索了几个小时,我们非常感谢任何帮助。注意,我不是很技术。

“carrier.json”具有以下内容,

{
    "Message360": {
        "ResponseStatus": 1,
        "Carrier": {
            "ApiVersion": "3",
            "CarrierSid": "c7e57a2a-92d7-0430",
            "AccountSid": "2f4ce81a-f08d-04e1",
            "PhoneNumber": "+19499999999",
            "Network": "Cellco Partnership dba Verizon Wireless - CA",
            "Wireless": "true",
            "ZipCode": "92604",
            "City": "Irvine",
            "Price": "0.0003",
            "Status": "success",
            "DateCreated": "2017-10-13 18:44:32"
        }
    }
}

我有一个名为carrier_json.go的go文件,其中包含以下信息。

package main

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

type Json struct {
    Message360 struct {
        ResponseStatus int `json:"ResponseStatus"`
        Carrier        struct {
            APIVersion  string `json:"ApiVersion"`
            CarrierSid  string `json:"CarrierSid"`
            AccountSid  string `json:"AccountSid"`
            PhoneNumber string `json:"PhoneNumber"`
            Network     string `json:"Network"`
            Wireless    string `json:"Wireless"`
            ZipCode     string `json:"ZipCode"`
            City        string `json:"City"`
            Price       string `json:"Price"`
            Status      string `json:"Status"`
            DateCreated string `json:"DateCreated"`
        } `json:"Carrier"`
    } `json:"Message360"`
}

func main() {
    // reading data from JSON File
    data, err := ioutil.ReadFile("carrier.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("./carrier.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.APIVersion, 10), obj.CarrierSid, obj.AccountSid)
        w.Write(record)
        record = nil
    }
    w.Flush()
}

1 个答案:

答案 0 :(得分:0)

有几个错误。

  1. carrier.json是一个对象而不是数组,但是您要将其解组为[]Json
  2. 函数strconv.FormatInt的第一个参数的类型为int64,但APIVersion的类型为string
  3. obj属于Json类型,没有名为APIVersion的元素。它应该是obj.Message360.Carrier.APIVersion
  4. 我想您想要写多个CSV记录,因为d是Json的一部分。您应该使用WriteAll而不是Write,因为Write只会写一条CSV记录。 (改进:为csv文件添加头部)
相关问题