使用Golang修改JSON文件
我试图在Golang中读入一个JSON文件,修改这个JSON文件,然后创建一个新的JSON文件/写入这个JSON文件。我在网上看到了几个样本,但似乎无法将二者结合起来得到所需的结果。我试着在GO中创建自己的JSON str并对其进行修改,但还是失败了使用Golang修改JSON文件,json,file,go,file-writing,Json,File,Go,File Writing,我试图在Golang中读入一个JSON文件,修改这个JSON文件,然后创建一个新的JSON文件/写入这个JSON文件。我在网上看到了几个样本,但似乎无法将二者结合起来得到所需的结果。我试着在GO中创建自己的JSON str并对其进行修改,但还是失败了 package main import ( "encoding/json" "fmt" ) type Person struct { Name string Age int Details
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string
Age int
Details interface{}
}
func main() {
//I created a simple Json structure here to play with
str := `{"name": "A",
"age":20,
"details": {"salary":100000000}
}`
var data Person
err := json.Unmarshal([]byte(str), &data)
if err != nil {
panic(err)
}
//I make a map so that I can adjust the value of the salary
details, ok := data.Details.(map[string]interface{})
if ok {
details["salary"] = 999999
}
//Change the other values of a Person
data.Name = "B"
data.Age = 19
fmt.Println(data)
//SAMPLE OUTPUT: {B 19 map[salary:999999]}
}
我已多次尝试读取该文件,以下是我的最佳尝试:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
/* Create the structure that follows the outline of the JSON file*/
type UserType struct {
User []struct {
CdbID string `json:"cdb_id"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Phone int64 `json:"phone"`
Email string `json:"email"`
Address []struct {
Street string `json:"street"`
City string `json:"city"`
Zip string `json:"zip"`
Country string `json:"country"`
} `json:"address"`
Authenticators []struct {
Name string `json:"name"`
Phone int64 `json:"phone"`
} `json:"authenticators"`
VoiceSig string `json:"voice_sig"`
VoicesigCreatedTime string `json:"voicesig_created_time"`
Status string `json:"status"`
} `json:"user"`
}
func main() {
file, err := ioutil.ReadFile("user.json") //Read File
if err != nil {
fmt.Print("Error:", err)
}
var u UserType
json.Unmarshal(file, &u) //Parse the Json-encoded Data and store the results in u
result, e := json.Marshal(u) //Returns the Json encoding of u into the variable result
if e != nil {
fmt.Println("error", err)
}
os.Stdout.Write(result) //The line of code golang.org uses to print the Json encoding
}
这是一个示例输出:
{
"user": [{
"cdb_id":"",
"firstname":"Tom",
"lastname":"Bradley",
"phone":14155555555,
"email":"tom@gmail.com",
"address":[{
"street":"4343 shoemaker ave",
"city":"Brea",
"zip":"92821",
"country":"USA"
}],
"authenticators":[{
"name":"Lisa Hayden",
"phone":15625555555
},{
"name":"Pavan M",
"phone":17145555555
}],
"voice_sig":"242y5-4546-555kk54-437879ek545",
"voicesig_created_time":"2017-08-02T21:27:44+0000",
"status":"verified"
}]
}
我只是对如何修改我想要的内容感到困惑,特别是前面提到的示例输出的“验证器”。谢谢 添加声明
type Authenticator struct {
Name string `json:"name"`
Phone int64 `json:"phone"`
}
改变
Authenticators []struct {
Name string `json:"name"`
Phone int64 `json:"phone"`
} `json:"authenticators"`
到
然后,要向第一个用户添加验证器:
u.User[0].Authenticators = append(u.User[0].Authenticators, Authenticator{
Name: "John Doe",
Phone: 1234567890,
})
我理解你的代码,但除非我搞砸了,否则事情就不对劲了。我得到的错误是:“#命令行参数。/json.go:48:u.User.Authenticators未定义”。我应该改变UserType的结构,而不是让那块文本替换成一系列更小的结构吗?谢谢,我重视你的回答!我错过了
u.User
是一个片段。我已经修改了上面的代码。
u.User[0].Authenticators = append(u.User[0].Authenticators, Authenticator{
Name: "John Doe",
Phone: 1234567890,
})