golang post请求带json数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 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 } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效