結構體生成Json
package main
import (
"encoding/json"
"fmt"
)
type IT struct {
Company string `json:"-"` //此欄位不會輸出到螢幕
//Company string `json:"company"` 這樣列印輸出別名首字母就會小寫(二次編碼)
Subjects []string `json:"subjects"` //二次編碼
IsOk bool `json:",string"`
Price float64 `json:",string"`
}
func main() {
//定義一個結構體變數,同時初始化
s := IT{"itcast", []string{"Golang", "PHP", "Java", "C++"}, true, 666.666}
//編碼,根據內容生成json文字
//buf, err := json.Marshal(s)
//buf = {"subjects":["Golang","PHP","Java","C++"],"IsOk":"true","Price":"666.666"}
buf, err := json.MarshalIndent(s, "", " ") //格式化編碼
if err != nil {
fmt.Println("err = ", err)
return
}
fmt.Println("buf = ", string(buf))
}
//結果
buf = {
"subjects": [ //首字母小寫
"Golang",
"PHP",
"Java",
"C++"
],
"IsOk": "true", //string型別
"Price": "666.666"
}
Json轉成結構體
package main
import (
"encoding/json"
"fmt"
)
type IT struct {
Address string `json:"address"`
Languages []string `json:"languages"` //二次編碼
Status bool `json:"isok"`
Price float64 `json:"price"`
}
func main() {
jsonBuf := `
{
"address": "北京",
"languages": [
"Golang",
"PHP",
"Java",
"Python"
],
"status": 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 = %+v\n", tmp)
type IT2 struct {
Subjects []string `json:"languages"` //二次編碼
}
var tmp2 IT2
err = json.Unmarshal([]byte(jsonBuf), &tmp2) //第二個引數要地址傳遞
if err != nil {
fmt.Println("err = ", err)
return
}
fmt.Printf("tmp2 = %+v\n", tmp2)
}
tmp = {Address:北京 Languages:[Golang PHP Java Python] Status:false Price:666.666}
tmp2 = {Subjects:[Golang PHP Java Python]}
Map生成Json
package main
import (
"encoding/json"
"fmt"
)
func main() {
//建立一個map
m := make(map[string]interface{}, 4) //因為型別多,可以用interface空介面
m["address"] = "北京"
m["languages"] = []string{"Golang", "PHP", "Java", "Python"}
m["status"] = true
m["price"] = 666.666
//編碼成json
//result, err := json.Marshal(m)
//result = {"address":"北京","languages":["Golang","PHP","Java","Python"],"price":666.666,"status":true}
result, err := json.MarshalIndent(m, "", " ")
if err != nil {
fmt.Println("err = ", err)
return
}
fmt.Println("result = ", string(result))
}
result = {
"address": "北京",
"languages": [
"Golang",
"PHP",
"Java",
"Python"
],
"price": 666.666,
"status": true
}
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 = %+v\n", 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 ============> %v\n", key, value)
switch data := value.(type) {
case string:
str = data
fmt.Printf("map[%s]的值型別為string, value = %s\n", key, str)
case bool:
fmt.Printf("map[%s]的值型別為bool, value = %v\n", key, data)
case float64:
fmt.Printf("map[%s]的值型別為float64, value = %f\n", key, data)
case []string:
fmt.Printf("map[%s]的值型別為[]string, value = %v\n", key, data)
case []interface{}:
fmt.Printf("map[%s]的值型別為[]interface, value = %v\n", key, data)
}
}
}
map[address]的值型別為string, value = 北京
map[Languages]的值型別為[]interface, value = [Golang PHP Java Python]
map[Status]的值型別為bool, value = true
map[price]的值型別為float64, value = 666.666000