This is a creation in Article, where the information may have evolved or changed.
Package Introduction
An official package that handles JSON (Encoding/json) is available for import.
Data
JSON is derived from the object structure of JavaScript, the data structure directly corresponding to the Golang, but the Golang map is also the 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.
structure |
Go |
JSON |
String |
String |
String |
Integral type |
int* |
Number |
Floating point |
float or double |
Number |
Array |
Slice |
Array |
Object |
struct |
Object |
Boolean |
bool |
bool |
Null value |
Nil |
Null |
How to convert go data into JSON
Basis
If you are using the official JSON library, you need 2 steps:
- When defining a data structure (when defining a struct, only the field name is capitalized, it is encoded into the JSON.) )
- Call JSON. Marshal Generating Data
Simple structure
Defined:
type Account struct { Email string password string Money float64}
Call
account := Account{ Email: "我我我我aha@q.com", password:"qqqqqqqq", Money: 200.33, } rs, err := json.Marshal(account) if err != nil { fmt.Println(err) } fmt.Println(rs) fmt.Println(string(rs))
Results:
[123 34 69 109 97 105 108 34 58 34 230 136 145 230 136 145 230 136 145 230 136 145 97 104 97 64 113 46 99 111 109 34 44 34 77 111 110 101 121 34 58 50 48 48 46 51 51 125]{"Email":"我我我我aha@q.com","Money":200.33}
Observe the results, you can find that the English characters and the ordinary character are transcoded into ASCII value substitution, and the characters that cannot be ASCII are used in 3 locations (such as the "I-I-me-i-aha@q.com" word in English and Chinese =>[34 230 136 145 230 136 145 230 136 145 230 136 145 97 104 97 64 113 46 99 111 109 34]), "230 136 145" means "I"
Composite structure
Personal understanding in Go is a type-centric language. A complex data structure is made up of a combination of several simple structures.
Therefore, the simple data structure can be turned, then the composite structure naturally is no problem.
Defined:
type User struct { Name string Age int Roles []string //slice Skill map[string]float64 //map Extra []interface{} //空类型}
Call
skill := make(map[string]float64) skill["python"] = 99.5 skill["elkxkr"] = 90 skill["ruby"] = 80.0 extra := []interface{}{"ha",skill} user := User{ Name:"战三", Age:28, Roles:[]string{"Owner","Master"}, Skill:skill, Extra:extra, } rs1, err1 := json.Marshal(user) if err1 != nil { fmt.Println(err1) } fmt.Println(rs1) fmt.Println(string(rs1))
Results
[123 34 78 97 109 101 34 58 34 230 136 152 228 184 137 34 44 34 65 103 101 34 58 50 56 44 34 82 111 108 101 115 34 58 91 34 79 119 110 101 114 34 44 34 77 97 115 116 101 114 34 93 44 34 83 107 105 108 108 34 58 123 34 101 108 107 120 107 114 34 58 57 48 44 34 112 121 116 104 111 110 34 58 57 57 46 53 44 34 114 117 98 121 34 58 56 48 125 44 34 69 120 116 114 97 34 58 91 34 104 97 34 44 123 34 101 108 107 120 107 114 34 58 57 48 44 34 112 121 116 104 111 110 34 58 57 57 46 53 44 34 114 117 98 121 34 58 56 48 125 93 125]{"Name":"战三","Age":28,"Roles":["Owner","Master"],"Skill":{"elkxkr":90,"python":99.5,"ruby":80},"Extra":["ha",{"elkxkr":90,"python":99.5,"ruby":80}]}
aliases [JSON tag]
Because the JSON world is usually lowercase, and go can only export uppercase fields to export, therefore, the way to provide aliases when defining the structure.
type Student struct { Name string `json:"name"`}可以理解成sql的 Name as name
When you have more than one field, you can set only the alias of a part of the field and set it on demand.
Decoding
According to the usual naming rules Marsshal is the encoding decoding is Unmarsshal (data []byte, v interface{})
user := User{}err = json.Unmarshal(jsonByte, &user)fmt.Println(user);
Results
{战三 28 [Owner Master] map[elkxkr:90 python:99.5 ruby:80] [ha map[elkxkr:90 python:99.5 ruby:80]]}
Other
The JSON tag also provides
1.omitempty mode
Like what
type Student struct { Name string `json:"name,omitempty"`}
The name is empty when no output is made
2.string
type Student struct { Age int `json:"age,string"`}
The result is a string string (age=3 = "Age": "3")
3. Other Packages
github.com/panthesingh/goson 提供复合数据的二次处理
Reference
Goexample
Human-golang processing JSON (i)---encoding