This is a creation in Article, where the information may have evolved or changed.
1. The relationship between string and []byte]
string and []byte can convert each other
s1 := "abcd" b1 := []byte(s1) fmt.Println(b1) // [97 98 99 100] s2 := "中文" b2 := []byte(s2)
2. JSON string to JSON
var dat map[string]interface{}if err := json.Unmarshal([]byte(newstr), &dat); err == nil {fmt.Println(dat["info"])}
The JSON string to JSON is converted by the Unmarsha method, accepting the []byte parameter, so we can convert the string to []byte] by []byte[string] and place the JSON string into the map
3, why to use Goson
The JSON string is converted to map[string]interface{} We will find that we have no way to traverse a field in JSON for further processing unless we have defined the struct. And Goson offers us a solution.
Github.com/panthesingh/goson
4. Regular matching
result := util.HttpGet(onlineurl)reg := regexp.MustCompile(`\{.+\}`)strslince := reg.FindAllString(result, -1)newstr := strings.Join(strslince, "")
The string after the regular match is []string format is not string format, we want to convert []string to string to further convert to JSON, I use string. Join method, there should be a better way