go操作json,注意json的嵌套、数组

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" }

type Object []interface{}

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"]} jsonStr = {"accessToken":"507b5e08ee444dck887b66bd08672905", "clientToken":"64e3a5415bfe405d9485f1jf2ea5c68e", "selectedProfile":{"id":"selID","name":"Bluek404"}, "availableProfiles":[{"id":"测试ava","name":"Bluek404"}]} //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)

	mapTmp := dat["selectedProfile"].(map[string]interface {})
	fmt.Println(mapTmp["id"])
	/*
	var dat2 map[string]interface{}
	if err := json.Unmarshal([]byte(jsonStr), &dat2); err == nil {
		fmt.Println( dat2["firstName"])
	}
	*/

	mapTmp2 := (dat["availableProfiles"].([]interface {}))[0].(map[string]interface {})
	//mapTmp3 := mapTmp2[0].(map[string]interface {})
	fmt.Println(mapTmp2["id"])
}

//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)
}

}

转载于:https://my.oschina.net/sqhua/blog/262341

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值