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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。