This is a creation in Article, where the information may have evolved or changed.
JSON parsing is the most indispensable aspect of today's (web) application development. Many languages require library support to parse and construct JSON, but Golang can do this well with native libraries.
There are two basic representations of JSON: struct and string. Analysis and construction is to solve string2struct and struct2string. In Golang, the library in charge of these two functions is located in "Encoding/json". The function usage instructions in the library can be viewed in "Https://gowalker.org/encoding/json". Below is a sample source for use.
JSON constructs:
Config. Host= "Http://www.baidu.com"
Config. port= "80"
If B,err:=json. Marshal (config); err==nil{
Fmt. Println (string (b))
}
JSON decoding
var jsonstr= ' {host ': ' http://www.baidu.com ', ' Port ': 80} '
If Err:=json. Unmarshal ([]byte (JSONSTR), &config); err==nil{
Fmt. Println (config)
}
Structure for storing JSON:
This point has some special, special instructions.
Type Configstruct struct{
Host string ' JSON: ' Host '
Port int ' JSON: ' Port '
}
Specifically, the member name associated with the JSON operation must start with a capital (that is, the lead), and add a string ' JSON: ' T (that is, name) after its type.