从 JSON 数据中提取特定信息的属性的使用方法
时间:2024-03-01
浏览:338
作者:佚名
小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《从 JSON 数据中提取特定信息的属性的使用方法》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!
问题内容
我被要求使用 go 做一个简单的 restful 服务,该服务必须使用其 id 值检索有关特定书籍的所有数据。
type book struct {
id string `json:"id"`
isbn string `json:"isbn"`
title string `json:"title"`
author *author `json:"author"`
}
//author struct
type author struct{
firstname string `json:"firstname"`
lastname string `json:"lastname"`
}
//variables
//slice-> variable link to array
var books []book
// get single book
func getbook(w http.responsewriter, r *http.request) {
w.header().set("content-type", "application/json")
params := mux.vars(r) // gets params
// loop through books and find one with the id from the params
for _, item := range books {
if item.id == params["id"] {
json.newencoder(w).encode(item)
return
}
}
json.newencoder(w).encode(&book{})
}
func main() {
// r := mux.newrouter()
fmt.println("hello api")
//initialize mux router
r := mux.newrouter()
//mock data
books=append(books,book{id:"1",isbn:"32123",title:"book one",author:&author{firstname:"j.k.", lastname:"rowling"}})
books=append(books,book{id:"2",isbn:"45434",title:"book two", author:&author{firstname:"p.k.",lastname:"rowling"}})
//router handler
r.handlefunc("/api/books",getbooks).methods("get")
r.handlefunc("/api/books/{id}",getbook).methods("get")
r.handlefunc("/api/books",createbook).methods("post")
r.handlefunc("/api/books/{id}",updatebook).methods("put")
r.handlefunc("/api/books/{id}",deletebook).methods("delete")
log.fatal(http.listenandserve(":8080",r))
}
预期输出 如果我输入 -> http://localhost:8080/api/books/1 这必须返回我
{
"id": "1",
"isbn": "32123",
"title": "Book one",
"author": {
"firstname": "J.K.",
"lastname": "Rowling"
}
}
相反,我在浏览器中什么也没有得到
解决方案
我只是将代码格式化如下,它工作正常(注意 writeheader 函数)。
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
type book struct {
id string `json:"id"`
isbn string `json:"isbn"`
title string `json:"title"`
author *author `json:"author"`
}
//author struct
type author struct {
firstname string `json:"firstname"`
lastname string `json:"lastname"`
}
//variables
//slice-> variable link to array
var books []book
// get single book
func getbook(w http.responsewriter, r *http.request) {
w.header().set("content-type", "application/json")
params := mux.vars(r) // gets params
// loop through books and find one with the id from the params
for _, item := range books {
if item.id == params["id"] {
w.writeheader(http.statusok)
json.newencoder(w).encode(item)
return
}
}
w.writeheader(http.statusnotfound)
json.newencoder(w).encode(&book{})
}
func main() {
fmt.println("listening on :8080")
// initialize mux router
r := mux.newrouter()
// mock data
books = append(books, book{id: "1", isbn: "32123", title: "book one", author: &author{firstname: "j.k.", lastname: "rowling"}})
books = append(books, book{id: "2", isbn: "45434", title: "book two", author: &author{firstname: "p.k.", lastname: "rowling"}})
// router handler
r.handlefunc("/api/books/{id}", getbook).methods("get")
log.fatal(http.listenandserve(":8080", r))
}
运行上面的程序后。在控制台上执行以下命令:
curl 127.0.0.1:8080/api/books/1
返回:
{"id":"1","isbn":"32123","title":"Book one","author":{"firstname":"J.K.","lastname":"Rowling"}}
最后,作为使用 golang 实现 rest api 的建议,您可以查看 here。
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
