JSON is a data format description language. A hardy structure of key and value, similar to an object in Javascript, a dictionary in Python. Typically, a JSON-formatted key is a string whose value can be any type, string, number, array, or object structure. Learn more about JSON with access to JSON.
Data structure Map
JSON is derived from the object structure of Javascript, Golang has a direct corresponding data structure map, but Golang map is also key-value structure, and the struct structure can also describe the JSON. Of course, for JSON data types, go also has the structure of the object to match. The approximate correspondence is as follows:
Data Type |
JSON |
Golang |
Word string |
String |
String |
Integer |
Number |
Int64 |
Floating point number |
Number |
Flaot64 |
Array |
Arrary |
Slice |
Object |
Object |
struct |
Boolean |
bool |
bool |
Null value |
Null |
Nil |
JSON-encoded basic structure encoding
Golang provides a standard library of Encoding/json for encoding json. It takes roughly two steps:
- First, you define the JSON structure body.
- Serialize using the Marshal method.
When defining a struct, only the field name is capitalized, and it is encoded into the JSON.
Type account struct {e -mail string password string float64}func main () {account : = account{ Email: "[email protected]", Password: "123456", money:100.5, } rs, err: = json. Marshal (account) if err! = nil{ log. Fatalln (Err) } fmt. Println (RS) fmt. Println (String (RS))}
You can see the output as follows, and the Marshal () method takes the parameters of an empty interface and returns a []byte structure. The lowercase named password field is not encoded into JSON, and the resulting JSON structure field is consistent with the account structure.
[123 34 69 109 97 105 108 34 58 34 114 115 106 50 49 55 64 103 109 97 105 108 46 99 111 109 34 44 34 77 111 110 101 121 34 125]{"Email": "[email protected]", "Money": 100.5}
Composite structure coding
Compared to string, digital and other basic data structure, slice slice, map dictionary is a composite structure. These structural encodings are similar. However, the key for map must be a string, and value must be of the same type of data.
Type User struct { Name string age int Roles []string Skill Map[string]float64} Func Main () { Skill: = Make (Map[string]float64) skill["python"] = 99.5 skill["Elixir"] = skill[" Ruby "] = 80.0 User: = user{ Name:" rsj217 ", age:27, Roles: []string{" Owner "," Master "}, Skill:skill, } rs, err: = json. Marshal (user) if err! = nil{ log. Fatalln (Err) } fmt. Println (String (RS))}
Output:
{ "Name": "rsj217", "age": " Roles": [ "Owner", "Master" ], "Skill": { "Elixir": +, "python": 99.5, "Ruby":
Nested encoding
Slice and map can match JSON arrays and objects, assuming, of course, that the object's value is the same type of case. More generally, the object's key can be a string, but its value can be multiple structures. Golang can implement this construct by defining the struct body:
Type User struct {Name stringage introles []stringskill map[string]float64account Account}type account struct {Email Stringmoney float64}func main () {skill: = map[string]float64{"Elixir": +, "python": 99.5, "Ruby": 80,}account: = Account{email: "[Email protected]", Money:102.3,}user: = user{name: "Wenjianbao", Age: 30,roles: []string{"Owner", "Master"},skill: skill,account:account,}js, err: = json. Marshal (user) if err! = Nil {log. Fatal (Err)}fmt. Println (String (JS))}
Output:
{ "Name": "Wenjianbao", "age": +, "Roles": [ "Owner", "Master" ], "Skill": { "Elixir": "Python": 99.5, "Ruby": "$" , "account ": { "email": "[email protected]", "Money": 102.3 }}
By defining the nested structure account, the structure of key and value is implemented. An array or slice of Golang, whose type is the same, if you encounter arrays of different data types, you need an empty structure:
Type User struct {Name stringage introles []stringskill map[string]float64account Accountextra []interface{}}type account struct {Email Stringmoney float64}func main () {skill: = map[string]float64{"Elixir": 90, "Python": 99.5, "Ruby": 80,}account: = Account{email: "[Email protected]", Money:102.3,}extra: = []interface{}{123, "Hello World"}user: = User{name: "Wenjianbao", Age: 30,roles: []string{"Owner", "Master"},skill: Skill,account:account,extra: extra,}js, err: = json. Marshal (user) if err! = Nil {log. Fatal (Err)}fmt. Println (String (JS))}
Output:
{ "Name": "Wenjianbao", "age": +, "Roles": [ "Owner", "Master" ], "Skill": { " Elixir ":", "python": 99.5, "Ruby": + , "account": { "email": "[email protected]", " Money ": 102.3 }, " Extra ": [ 123, ' Hello World ' ]}
Using an empty interface, you can also define a dictionary structure that implements that different value type like a struct. When the null interface does not initialize its value, the 0 value is nil. Encoded as JSON is NULL
Type User struct {Name stringage introles []stringskill map[string]float64account Accountextra [] Interface{}level map[string]interface{}}type account struct {Email Stringmoney float64}func main () {skill: = map[string] float64{"Elixir": "Python": 99.5, "Ruby": 80,}account: = Account{email: "[Email protected]", Money:102.3,}level : = Make (map[string]interface{}) level["web"] = "good" level["server"] = 90level["tool"] = Niluser: = user{name: " Wenjianbao ", Age: 30,roles: []string{" Owner "," Master "},skill: skill,account:account,level: Level,}js, err: = json. Marshal (user) if err! = Nil {log. Fatal (Err)}fmt. Println (String (JS))}
Output:
{ "Name": "Wenjianbao", "age": +, "Roles": [ "Owner", "Master" ], "Skill": { " Elixir ":", "python": 99.5, "Ruby": + , "account": { "email": "[email protected]", " Money ": 102.3 }, " Extra ": null, " level ": { " server ": $, " tool ": null, " Web ":" Good " }}
You can see that the extra returned is not an empty slice, but null. The level field also implements a nested structure to the dictionary.
Golang processing Json (i): encoding