package main import ( "encoding/json" "fmt" ) type JsonPost struct { Org_code string `json:"org_code"` Org_index string `json:"org_index"` Org_name string `json:"org_name"` Place_code string `json:"place_code"` Resource_type string `json:"resource_type"` Access_type string `json:"access_type"` Producer string `json:"producer"` Producer_name string `json:"producer_name"` Register_id string `json:"register_id"` Register_pwd string `json:"register_pwd"` Ip_addr string `json:"ip_addr"` Port int `json:"port"` User_id string `json:"user_id"` Password string `json:"password"` Name string `json:"name"` Model string `json:"model"` Sub_type string `json:"sub_type"` Place string `json:"place"` Password_encode string `json:"password_encode"` Description string `json:"description"` Domain_swith string `json:"domain_swith"` Function_type string `json:"function_type"` Position_type string `json:"position_type"` Is_sdc string `json:"is_sdc"` Sdk_type string `json:"sdk_type"` Channel_num int `json:"channel_num"` Channel_list []ChildPost `json:"channel_list"` } type ChildPost struct { Ape_id string `json:"ape_id"` Place_code string `json:"place_code"` Enabled string `json:"enabled"` Idx int `json:"idx"` Name string `json:"name"` Model string `json:"model"` Longitude string `json:"longitude"` Latitude string `json:"latitude"` Place string `json:"place"` Resource_type string `json:"resource_type"` Sub_type string `json:"sub_type"` } func MakeData(ip, org_i, org_n, name1, ch_name string) []byte { body := `{ "org_code": "", "org_index": "10", "org_name": "本域", "place_code": "100000", "resource_type": "1", "access_type": "1", "producer": "1", "producer_name": "", "register_id": "", "register_pwd": "", "ip_addr": "10.100.0.11", "port": 3000, "user_id": "admin", "password": "admin", "name": "sdk_000001", "model": "", "sub_type": "1", "place": "", "password_encode": "", "description": "", "domain_swith": "", "function_type": "", "position_type": "", "is_sdc": "0", "sdk_type": "33", "channel_num": 1, "channel_list": [{ "ape_id": "", "place_code": "100000", "enabled": "1", "idx": 0, "name": "sdk_000001_通道_1", "model": "3", "longitude": "", "latitude": "", "place": "", "resource_type": "6", "sub_type": "1" }] }` var j JsonPost err := json.Unmarshal([]byte(body), &j) //反序列化 if err != nil { fmt.Println("err was %v", err) } j.Ip_addr = ip j.Org_index = org_i j.Org_name = org_n j.Name = name1 j.Channel_list[0].Name = ch_name //修改相关字段 fmt.Println(j) data, err := json.Marshal(j) //改完字段再序列化回来 if err != nil { fmt.Println("err was %v", err) } fmt.Println(string(data)) return data }