golang的json操作
2015-12-06 22:48
716 查看
package main import ( "encoding/json" "fmt" "os" ) type ConfigStruct struct { Host string `json:"host"` Port int `json:"port"` AnalyticsFile string `json:"analytics_file"` StaticFileVersion int `json:"static_file_version"` StaticDir string `json:"static_dir"` TemplatesDir string `json:"templates_dir"` SerTcpSocketHost string `json:"serTcpSocketHost"` SerTcpSocketPort int `json:"serTcpSocketPort"` Fruits []string `json:"fruits"` } type Other struct { SerTcpSocketHost string `json:"serTcpSocketHost"` SerTcpSocketPort int `json:"serTcpSocketPort"` Fruits []string `json:"fruits"` } func main() { jsonStr := `{"host": "http://localhost:9090","port": 9090,"analytics_file": "","static_file_version": 1,"static_dir": "E:/Project/goTest/src/","templates_dir": "E:/Project/goTest/src/templates/","serTcpSocketHost": ":12340","serTcpSocketPort": 12340,"fruits": ["apple", "peach"]}` //json str 转map var dat map[string]interface{} if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil { fmt.Println("==============json str 转map=======================") fmt.Println(dat) fmt.Println(dat["host"]) } //json str 转struct var config ConfigStruct if err := json.Unmarshal([]byte(jsonStr), &config); err == nil { fmt.Println("================json str 转struct==") fmt.Println(config) fmt.Println(config.Host) } //json str 转struct(部份字段) var part Other if err := json.Unmarshal([]byte(jsonStr), &part); err == nil { fmt.Println("================json str 转struct==") fmt.Println(part) fmt.Println(part.SerTcpSocketPort) } //struct 到json str if b, err := json.Marshal(config); err == nil { fmt.Println("================struct 到json str==") fmt.Println(string(b)) } //map 到json str fmt.Println("================map 到json str=====================") enc := json.NewEncoder(os.Stdout) enc.Encode(dat) //array 到 json str arr := []string{"hello", "apple", "python", "golang", "base", "peach", "pear"} lang, err := json.Marshal(arr) if err == nil { fmt.Println("================array 到 json str==") fmt.Println(string(lang)) } //json 到 []string var wo []string if err := json.Unmarshal(lang, &wo); err == nil { fmt.Println("================json 到 []string==") fmt.Println(wo) } }
//go post方式提交
//导入:"net/http" "crypto/tls" //调用片段: ////machineArry jsonMachine, err := json.Marshal(machineArry) if err == nil { fmt.Println("================array 到 json str==") fmt.Println(string(jsonMachine)) } //?link_token="+token_updatemachine token_updatemachine :=beego.AppConfig.String("token_updatemachine") mappara :=map[string]interface{}{} mappara["updateJson"]=string(jsonMachine) mappara["link_token"]=token_updatemachine masterUrl222 := beego.AppConfig.String("masterUrl") url222 := masterUrl222+"/cmdb/cmdbapi/updatemachine" resmap :=this.HttpSendByJsonPost(url222,mappara) if resmap.(map[string]interface{})["code"].(string) =="000" { return updatemap }else{ updatemap["_code"] = "1" updatemap["_msg"] = resmap.(map[string]interface{})["result"] return updatemap } func (this *BaseController) HttpSendByJsonPost(url string,formData map[string]interface{}) interface{} { req := httplib.Post(url) req.SetTransport(&http.Transport{DisableKeepAlives: true}) for k, v := range formData { req.Param(k, v.(string)) } req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) //req.Header("user", user) //req.Header("role", role) //req.SetHost() var result interface{} req.ToJson(&result) fmt.Println("resultresultresultresultresult:--------------->>>>>>>",result) return result }
相关文章推荐
- 用Javascript获取页面元素的位置
- phaser.js完全不能运行,webgl的支持还需要加强啊!!
- 一个简单的json数组解析(阿里巴巴)
- js之数据类型-隐式转换-类型检测 学习总结
- javascript新建对象及基本数据类型
- js浏览器控制台不报错,但不运行下面的js代码
- fastjson
- JavaScript 简单的计算器
- JSP生成Excel报表
- Grunt插件jshint使用指南
- J2EE——Jsp的学习
- JS生成登录验证码
- JavaScript:wihdow.onload与DOMContentLoaded,onreadystate
- JavaScript-数字和字符串比较大小
- JavaScript的学习要点
- JavaScript 数组乱序方法
- javascript必知必会之prototype
- Microsoft 开源 Edge 浏览器的 javascript 引擎
- Java运用JSON实现后台与前端分工合作(代码实例)
- Javascript继承的概念理解