首頁 > 軟體

go語言通過結構體生成json範例解析

2022-04-14 19:00:37

通過結構體生成json

buf, err := json.MarshalIndent(s, "", " ") //格式化編碼

package main
import (
	"encoding/json"
	"fmt"
)
//成員變數名首字母必須大寫
type IT struct {
	Company  string   `json:"-"`        //此欄位不會輸出到螢幕
	Subjects []string `json:"subjects"` //二次編碼
	IsOk     bool     `json:",string"`
	Price    float64  `json:",string"`
}
func main() {
	//定義一個結構體變數,同時初始化
	s := IT{"itcast", []string{"Go", "C++", "Python", "Test"}, true, 666.666}
	//編碼,根據內容生成json文字
	//{"Company":"itcast","Subjects":["Go","C++","Python","Test"],"IsOk":true,"Price":666.666}
	//buf, err := json.Marshal(s)
	buf, err := json.MarshalIndent(s, "", "	") //格式化編碼
	if err != nil {
		fmt.Println("err = ", err)
		return
	}
	fmt.Println(string(buf))
}

通過map生成json

package main
import (
	"encoding/json"
	"fmt"
)
func main() {
	//建立一個map
	m := make(map[string]interface{}, 4)
	m["company"] = "itcast"
	m["subjects"] = []string{"Go", "C++", "Python", "Test"}
	m["isok"] = true
	m["price"] = 666.666
	//編碼成json
	//result, err := json.Marshal(m)
	result, err := json.MarshalIndent(m, "", "	")
	if err != nil {
		fmt.Println("err = ", err)
		return
	}
	fmt.Println("result = ", string(result))
}

json解析到結構體

err := json.Unmarshal([]byte(jsonBuf), &tmp) //第二個引數要地址傳遞

package main
import (
	"encoding/json"
	"fmt"
)
type IT struct {
	Company  string   `json:"company"`
	Subjects []string `json:"subjects"` //二次編碼
	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                                   //定義一個結構體變數
	err := json.Unmarshal([]byte(jsonBuf), &tmp) //第二個引數要地址傳遞
	if err != nil {
		fmt.Println("err = ", err)
		return
	}
	//fmt.Println("tmp = ", tmp)
	fmt.Printf("tmp = %+vn", tmp)

	type IT2 struct {
		Subjects []string `json:"subjects"` //二次編碼
	}
	var tmp2 IT2
	err = json.Unmarshal([]byte(jsonBuf), &tmp2) //第二個引數要地址傳遞
	if err != nil {
		fmt.Println("err = ", err)
		return
	}
	fmt.Printf("tmp2 = %+vn", tmp2)
}

json解析到map

package main
import (
	"encoding/json"
	"fmt"
)
func main() {
	jsonBuf := `
	{
    "company": "itcast",
    "subjects": [
        "Go",
        "C++",
        "Python",
        "Test"
    ],
    "isok": true,
    "price": 666.666
}`
	//建立一個map
	m := make(map[string]interface{}, 4)
	err := json.Unmarshal([]byte(jsonBuf), &m) //第二個引數要地址傳遞
	if err != nil {
		fmt.Println("err = ", err)
		return
	}
	fmt.Printf("m = %+vn", m)
	//	var str string
	//	str = string(m["company"]) //err, 無法轉換
	//	fmt.Println("str = ", str)
	var str string
	//型別斷言, 值,它是value型別
	for key, value := range m {
		//fmt.Printf("%v ============> %vn", key, value)
		switch data := value.(type) {
		case string:
			str = data
			fmt.Printf("map[%s]的值型別為string, value = %sn", key, str)
		case bool:
			fmt.Printf("map[%s]的值型別為bool, value = %vn", key, data)
		case float64:
			fmt.Printf("map[%s]的值型別為float64, value = %fn", key, data)
		case []string:
			fmt.Printf("map[%s]的值型別為[]string, value = %vn", key, data)
		case []interface{}:
			fmt.Printf("map[%s]的值型別為[]interface, value = %vn", key, data)
		}
	}
}

以上就是go語言通過結構體生成json範例解析的詳細內容,更多關於go通過結構體生成json的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com