doufei5537
2017-12-20 03:23
浏览 430

正确创建一个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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用Golang创建JSON文件。 我对JSON文件和创建这些文件知之甚少,但是我创建了一个创建它们的程序。 在此程序中,它从网站获取表单数据,然后将数据放入JSON结构中,然后将信息添加到文件夹中。 我这里有2个数据部分。 我在发生错误的地方加上了注释

  {
“ cl”:“ [v1]”,
“ gr”:“ [A]”  ,
“ cr”:“ [8]” 
} //预计文件结尾
 {
“ cl”:“ [v2]”,
“ gr”:“ [Z]”,
  “ cr”:“ [8]” 
} 
   
 
 

所以我的问题是(1)错误是什么意思,以及(2)我如何/如何 使用Golang创建JSON文件时解决此问题? 如果需要,我可以提供Golang。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
    
    
    }
    
    打赏 评论

相关推荐 更多相似问题