// convert json to struct
package main
import (
"fmt"
"encoding/json"
)
type Host struct {
IP string
Name string
}
func main() {
b := []byte(`{"IP": "192.168.11.22","name": "SKY"}`)
m := Host{}
err := json.Unmarshal(b,&m)
if err != nil {
fmt.Println("Umarshal Failed:",err)
return
}
fmt.Println("m:",m)
fmt.Println("m.IP:",m.IP)
fmt.Println("m.Name:",m.Name)
}
output:
原文链接:https://www.f2er.com/go/187522.htmlm: {192.168.11.22 SKY} m.IP: 192.168.11.22 m.Name: SKY