{
"servers": [
{
"serverName": "Shanghai_VPN",
"serverIP": "127.0.0.1"
},
{
"serverName": "Beijing_VPN",
"serverIP": "127.0.0.2"
}
]
}
Parse
func Unmarshal(data []byte, v interface{}) error
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Servers []Server
}
func main() {
var s Serverslice
str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}]}`
json.Unmarshal([]byte(str), &s)
fmt.Println(s)
}
Parse to interface{}
Without knowing the type of json data
-
bool for JSON booleans,
-
float64 for JSON numbers,
-
string represents JSON strings,
-
nil stands for JSON null.
func main() { var f interface{} b := []byte() if err := json.Unmarshal(b, &f); err != nil { log.Fatalln(err) }
{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}
m := f.(map[string]interface{}) for k, v := range m { switch vtype := v.(type) { case int: fmt.Println(k, "is int: ", vtype) case string: fmt.Println(k, "is string: ", vtype) case float64: fmt.Println(k, "is float64: ", vtype) case []interface{}: fmt.Println(k, "is array: ", vtype) default: fmt.Println(k, "is unrecognized: ", vtype) } }
}
Also available simplejson package
generate json
func Marshal(v interface{}) ([]byte, error)
-
The tag of the field is "-", then this field will not be output to JSON
-
If the tag has a custom name, then the custom name will appear in the JSON field name, such as serverName in the above example
-
If the tag has the "omitempty" option, then if the field value is empty, it will not be output to the JSON string
-
If the field type is bool, string, int, int64, etc., and the tag contains the ",string" option, then the field will convert the corresponding value of the field into a JSON string when outputting to JSON.
type Server struct { // ID will not be exported to JSON ID int
json:"-"
// ServerName2 的值会进行二次 JSON 编码 ServerName string `json:"serverName"` ServerName2 string `json:"serverName2,string"` // 如果 ServerIP 为空,则不输出到 JSON 串中 ServerIP string `json:"serverIP,omitempty"`
}
s := Server { ID: 3, ServerName: , ServerName2: , ServerIP: ``, } b, _ := json.Marshal(s) os.Stdout.Write(b)
Go "1.0"
Go "1.0"
//stdout
{“serverName”:"Go “1.0” “,“serverName2”:”"Go “1.0” “”}
Summarize
This is the end of writing, and I put a small benefit at the end of the article. The following is a learning idea and direction about java development that I have sorted out during the learning process. In Internet development, the most important thing is to learn technology well, and learning technology is a slow, long and arduous road. You can’t rely on passion for a while, and you can’t learn it well by staying up for a few days and nights. You must develop the habit of studying hard. More need for accurate learning direction to achieve effective learning effect.
Since there is a lot of content, only a general outline is put on it. If you need a more detailed learning mind map, click on my Gitee to get it .
There is also a full set of advanced java video tutorials java advanced architect video + information + code + interview questions!
All aspects of java advanced practice technical materials, and there are also technical experts to discuss and exchange problems to solve.