33 lines
548 B
Go
33 lines
548 B
Go
package restful
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
)
|
|
|
|
type Todo struct{
|
|
UserId int `json:"userId"`
|
|
Id int `json:"id"`
|
|
Title string `json:"title"`
|
|
Completed bool `json:"completed"`
|
|
}
|
|
|
|
func Demo1() {
|
|
response, err := http.Get("https://jsonplaceholder.typicode.com/todos/1")
|
|
if err != nil{
|
|
fmt.Println(err)
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
bodyBytes,_ := ioutil.ReadAll(response.Body)
|
|
|
|
bodyString := string(bodyBytes)
|
|
fmt.Println(bodyString)
|
|
|
|
var todo Todo
|
|
json.Unmarshal(bodyBytes, &todo)
|
|
fmt.Println(todo)
|
|
} |