golang json

前端之家收集整理的这篇文章主要介绍了golang json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
packagemain

import(
	"encoding/json"
	"fmt"
	"os"
)

typeConfigStructstruct{
	Expires_instring`json:"expires_in"`
	Refresh_tokenstring`json:"refresh_token"`
	Access_tokenstring`json:"access_token"`
	Session_secretstring`json:"session_secret"`
	Scopestring`json:"scope"`
	Session_keystring`json:"session_key"`
}

funcmain(){
	jsonStr:=`{expires_in":2592000,"refresh_token":"22.1e1285e0ab8cb8e4453d31cb53774369.315360000.1756022822.2605827041-6701125","access_token":"21.9ab5cd9698424e9f56e0a8488aabd9f0.2592000.1443254822.2605827041-6701125","session_secret":"9985f4f820286dae4c532fbfb111dc91","session_key":"9mnRfVsV+0JrbIB15+MNzaUm3YyLsqOrRWvBODzwG3KK5RaRGMlLx+smjfkMLmrwlvItGhDln9OxrNnrjJZ3yIUTZ2K0zkJkEA==","scope":"basic"}`

	//tomap
	vardatamap[string]interface{}
	iferr:=json.Unmarshal([]byte(jsonStr),&data);err==nil{
		fmt.Println(data)
	}

	//jsontostruct
	varconfigConfigStruct
	iferr:=json.Unmarshal([]byte(jsonStr),&config);err==nil{
		fmt.Println(config)
		fmt.Println(config.Access_token)
	}

	//structtojsonstr
	ifb,err:=json.Marshal(config);err==nil{
		fmt.Println(string(b))
	}

	//maptojsonstr
	enc:=json.NewEncoder(os.Stdout)
	enc.Encode(data)

	//arraytojsonstr
	arr:=[]string{"c","c++","java","python","javaScript","nodejs","golang"}
	lang,err:=json.Marshal(arr)
	iferr==nil{
		fmt.Println(string(lang))
	}

	//jsonto[]string
	varwo[]string
	iferr:=json.Unmarshal(lang,&wo);err==nil{
		fmt.Println(wo)
	}
}

猜你在找的Go相关文章