SoFunction
Updated on 2025-03-03

Go language generates json example analysis through structure

Generate json through structure

buf, err := (s, "", " ") //Format encoding

package main
import (
	"encoding/json"
	"fmt"
)
//The first letter of the member variable must be capitalizedtype IT struct {
	Company  string   `json:"-"`        //This field will not be output to the screen	Subjects []string `json:"subjects"` //Second Coding	IsOk     bool     `json:",string"`
	Price    float64  `json:",string"`
}
func main() {
	//Define a structure variable and initialize it at the same time	s := IT{"itcast", []string{"Go", "C++", "Python", "Test"}, true, 666.666}
	//Encoding, generate json text based on the content	//{"Company":"itcast","Subjects":["Go","C++","Python","Test"],"IsOk":true,"Price":666.666}
	//buf, err := (s)
	buf, err := (s, "", "	") //Format code	if err != nil {
		("err = ", err)
		return
	}
	(string(buf))
}

Generate json via map

package main
import (
	"encoding/json"
	"fmt"
)
func main() {
	//Create a map	m := make(map[string]interface{}, 4)
	m["company"] = "itcast"
	m["subjects"] = []string{"Go", "C++", "Python", "Test"}
	m["isok"] = true
	m["price"] = 666.666
	//Encoded into json	//result, err := (m)
	result, err := (m, "", "	")
	if err != nil {
		("err = ", err)
		return
	}
	("result = ", string(result))
}

json parsing to structure

err := ([]byte(jsonBuf), &tmp) //The second parameter needs to be passed in address

package main
import (
	"encoding/json"
	"fmt"
)
type IT struct {
	Company  string   `json:"company"`
	Subjects []string `json:"subjects"` //Second Coding	IsOk     bool     `json:"isok"`
	Price    float64  `json:"price"`
}
func main() {
	jsonBuf := `
	{
    "company": "itcast",
    "subjects": [
        "Go",
        "C++",
        "Python",
        "Test"
    ],
    "isok": true,
    "price": 666.666
}`
	var tmp IT                                   //Define a structure variable	err := ([]byte(jsonBuf), &tmp) //The second parameter needs to be passed on the address	if err != nil {
		("err = ", err)
		return
	}
	//("tmp = ", tmp)
	("tmp = %+v\n", tmp)

	type IT2 struct {
		Subjects []string `json:"subjects"` //Second Coding	}
	var tmp2 IT2
	err = ([]byte(jsonBuf), &tmp2) //The second parameter needs to be passed on the address	if err != nil {
		("err = ", err)
		return
	}
	("tmp2 = %+v\n", tmp2)
}

json parsing to map

package main
import (
	"encoding/json"
	"fmt"
)
func main() {
	jsonBuf := `
	{
    "company": "itcast",
    "subjects": [
        "Go",
        "C++",
        "Python",
        "Test"
    ],
    "isok": true,
    "price": 666.666
}`
	//Create a map	m := make(map[string]interface{}, 4)
	err := ([]byte(jsonBuf), &m) //The second parameter needs to be passed on the address	if err != nil {
		("err = ", err)
		return
	}
	("m = %+v\n", m)
	//	var str string
	// str = string(m["company"]) //err, cannot be converted	//	("str = ", str)
	var str string
	//Type assertion, value, it is type value	for key, value := range m {
		//("%v ============> %v\n", key, value)
		switch data := value.(type) {
		case string:
			str = data
			("map[%s]The value type isstring, value = %s\n", key, str)
		case bool:
			("map[%s]The value type isbool, value = %v\n", key, data)
		case float64:
			("map[%s]The value type isfloat64, value = %f\n", key, data)
		case []string:
			("map[%s]The value type is[]string, value = %v\n", key, data)
		case []interface{}:
			("map[%s]The value type is[]interface, value = %v\n", key, data)
		}
	}
}

The above is the detailed analysis of the example analysis of Go language using structure generation json. For more information about go to generate json through structure, please pay attention to my other related articles!