doufei5537 2017-12-20 03:23
浏览 565
已采纳

正确创建一个JSON文件并从中读取

I am trying to create a JSON file with Golang. I have little knowledge of JSON Files and creating them but I have created a program that creates them. In this program, it takes form data from a website and then puts the data into a JSON struct, then adds the information into a folder. I have the 2 sections of data here. I put a comment where the error occurs along with the error

{
    "cl":"[v1]",
    "gr":"[A]",
    "cr":"[8]"
} // End Of File Expected
{
    "cl":"[v2]",
    "gr":"[Z]",
    "cr":"[8]"
}

So my questions are (1) What does the error mean, and (2) How/can I fix this when creating a JSON file with Golang? I can supply the Golang if needed.

  • 写回答

1条回答 默认 最新

  • duancongduo4109 2017-12-20 03:56
    关注

    So other than the json not being formatted correctly, here is an example of how to create json using a struct and json struct tags.

    Proper JSON

    [ {key:value, key value}, {key:value, key value} ]

    What you have is {key:value, key value} {key:value, key value}

    Which is two separate objects instead of one object in an array.

    If you are reading this from file and the data is returned like your example then you might have to split on the newline to separate each object and unmarshal them separately.

    Otherwise the below should server as an example.

    package main
    
    import (
        "encoding/json"
        "fmt"
        "io/ioutil"
        "strconv"
    )
    
    type j struct {
        Cl []string `json:"cl"`
        Gr []string `json:"gr"`
        Cr []string `json:"cr"`
    }
    
    func main() {
        // create an instance of j as a slice
        var data []j
        // using a for loop for create dummy data fast
        for i := 0; i < 5; i++ {
            v := strconv.Itoa(i)
            data = append(data, j{
                Cl: []string{"foo " + v},
                Gr: []string{"bar " + v},
                Cr: []string{"foobar " + v},
            })
        }
    
        // printing out json neatly to demonstrate
        b, _ := json.MarshalIndent(data, "", " ")
        fmt.Println(string(b))
    
        // writing json to file
    
        _ = ioutil.WriteFile("file.json", b, 0644)
    
        // to append to a file
        // create the file if it doesn't exists with O_CREATE, Set the file up for read write, add the append flag and set the permission
        f, err := os.OpenFile("/var/log/debug-web.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0660)
        if err != nil {
            log.Fatal(err)
        }
        // write to file, f.Write()
        f.Write(b)
    
        // if you are doing alot of I/O work you may not want to write out to file often instead load up a bytes.Buffer and write to file when you are done... assuming you don't run out of memory when loading to bytes.Buffer
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题