2,187
社区成员
发帖
与我相关
我的任务
分享
import (
"os"
"encoding/json"
"io/ioutil"
)
type yourjson strut {
//difine your json struct
}
func main() { //ignore error handler
yourfile, _ := os.Open("yourjson.json")
defer yourfile.Close()
bs, _ := ioutil.ReadAll(yourfile) //read json file to bytes
js := &yourjson{}
json.Unmarshal(bs, js) //conver json bytes to struct
js.xxx = yyy //modify your json struct
newbs, _ := json.MarshalIndent(js, "", " ") //convert struct to bytes
ioutil.WriteFile("yournewjson.json", newbs, 0644) //write json bytes to file
}