如今不管是网站、App
、小程序仍是移动端H5
页面应用,都是采用前端与后端单独部署,相互之间以API
接口交互的形式构建而成的。由于在结合可读性、编码数据大小和开发者使用难度上都JSON
格式是一个比较好的选择,因此接口的数据格式一般都采用JSON
,即前端在发送POST
,PUT
,PATCH
请求添加,更改数据时会把数据以JSON
格式放到请求的Body
中。然后端则是全部数据都会以JSON
格式返回。前端
关于JSON
可读性、编码数据大小和开发者使用难度上,由于其可读性不如XML
结构,可是数据量小,用程序操做起来更方便。对比Protobuf
来讲,Protobuf
编码速度、编码后数据大小比JSON
都要好,可是用程序操做起来没有JSON
方便简单,编码后的数据是二进制格式的,易读性彻底没有。因此总体来讲JSON
是一个各个方面都不错更容易被全部人接受才被普遍使用的(以上都是我的观点)。shell
以前也写过两篇关于用Go
语言解码和编码JSON
数据的文章编程
那么针对Web
编程咱们其实只要关注怎么从HTTP
请求的Body
中读取到JSON
数据,以及如何将要返回给客户端的数据以JSON
格式写入到HTTP
响应中。json
关于这部份内容其实在以前的文章深刻学习解析HTTP请求里有说过。小程序
咱们须要把请求体做为json.NewDecoder()
的输入流,而后将请求体中携带的JSON
格式的数据解析到声明的结构体变量中后端
//handler/parse_json_request package handler import ( "encoding/json" "fmt" "net/http" ) type Person struct { Name string Age int } func DisplayPersonHandler(w http.ResponseWriter, r *http.Request) { var p Person // 将请求体中的 JSON 数据解析到结构体中 // 发生错误,返回400 错误码 err := json.NewDecoder(r.Body).Decode(&p) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } fmt.Fprintf(w, "Person: %+v", p) } // router/router.go indexRouter.HandleFunc("/parse_json_request", handler.ParseJsonRequestHandler)
在命令行里用cURL
命令测试咱们的程序:服务器
curl -X POST -d '{"name": "James", "age": 18}' \ -H "Content-Type: application/json" \ http://localhost:8000/index/parse_json_request
与上面相反,将返回数据以JSON
格式写入响应时,咱们调用json.NewEncodeer(w).Encode(&v)
,用响应体做为输入流建立JSON
编码器,而后使用其Encode()
方法将数据编码为JSON
格式并写入响应体。app
// handler/write_json_response package handler import ( "encoding/json" "net/http" ) type User struct { FirstName string `json:"firstname"` LastName string `json:"lastname"` Age int `json:"age"` } func WriteJsonResponseHandler(w http.ResponseWriter, r *http.Request) { p := User{ FirstName: "John", LastName: "Doe", Age: 25, } // Set response header w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(&p) if err != nil { //... handle error } } // router/router.go indexRouter.HandleFunc("/get_json_response", handler.WriteJsonResponseHandler)
重启服务器后在命令行里用cURL
命令测试咱们的程序:curl
curl -X GET http://localhost:8000/index/get_json_response {"firstname":"John","lastname":"Doe","age":25}
今天的内容很简单,源码已经上传,公众号回复gohttp12获取文中源代码的下载连接。学习
前文回顾