英文:
Parse many data from api to html with golang
问题
我是Go语言的新手,所以我有一个问题。我有一个API,我需要从中解析信息到卡片中。我有一个脚本,它从API中获取所有信息,并将其放入卡片的字段中。我知道如何获取一部分信息。但是我需要根据API中的不同信息创建多个具有不同信息的卡片,并将它们放置在HTML页面上。我该如何做到这一点?
server.go文件用于HTML托管。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
)
type Response struct {
Artists string `json:"artists"`
Locations string `json:"locations"`
Dates string `json:"dates"`
Relation string `json:"relation"`
}
type Artist struct {
ID int `json:"id"`
Image string `json:"image"`
Name string `json:"name"`
Members []string `json:"members"`
CreationDate string `json:"creationDate"`
FirstAlbum string `json:"firstAlbum"`
}
func main() {
http.HandleFunc("/", formHandler) // 定义我们将使用的路径/页面,以及我们将使用的函数
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil { // 在端口8080上初始化我们的服务器
log.Fatal("HTTP status 500 - Internal server error: %s", err)
}
}
func formHandler(w http.ResponseWriter, r *http.Request) {
response, err := http.Get("https://groupietrackers.herokuapp.com/api")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var responseObject Response
json.Unmarshal(responseData, &responseObject)
artist_link, err := http.Get(responseObject.Artists)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
artistData, err := ioutil.ReadAll(artist_link.Body)
if err != nil {
log.Fatal(err)
}
var artistObject Artist
json.Unmarshal(artistData, &artistObject)
t, _ := template.ParseFiles("index.html")
t.Execute(w, artistObject)
}
index.html文件用于向用户显示信息。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<style>
iframe {
width: 99vw;
}
.card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
width: 40%;
}
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
.container {
padding: 2px 16px;
}
</style>
<body>
<header>
<h1>groupie-tracker</h1>
</header>
<div>
{{range .}}
<div class="card">
<div class="container">
<h4><b>{{ .Name }}</b></h4>
<p>{{ .FirstAlbum }}</p>
</div>
</div>
{{end}}
</div>
</body>
</html>
英文:
I am new in Go language so I have a question. I have API from where I need to parse information to cards. I have a script which GET all information from API and place it into fields in card. I understand how to take one part of information. But I need to create many cards with different information from API and place them on HTML page. How I can do this?
server.go file for HTML hosting.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
)
type Response struct {
Artists string `json:"artists"`
Locations string `json:"locations"`
Dates string `json:"dates"`
Relation string `json:"relation"`
}
type Artist struct {
ID int `json:"id"`
Image string `json:"image"`
Name string `json:"name"`
Members []string `json:"members"`
CreationDate string `json:"creationDate"`
FirstAlbum string `json:"firstAlbum"`
}
func main() {
http.HandleFunc("/", formHandler) // to define the path/page, where we are going to use the mentioned function
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil { //to initialise our server on :8080
log.Fatal("HTTP status 500 - Internal server error: %s", err)
}
}
func formHandler(w http.ResponseWriter, r *http.Request) {
response, err := http.Get("https://groupietrackers.herokuapp.com/api")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var responseObject Response
json.Unmarshal(responseData, &responseObject)
artist_link, err := http.Get(responseObject.Artists)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
artistData, err := ioutil.ReadAll(artist_link.Body)
if err != nil {
log.Fatal(err)
}
var artistObject Artist
json.Unmarshal(artistData, &artistObject)
t, _ := template.ParseFiles("index.html")
t.Execute(w, artistObject)
}
index.html file for showing information to pearson.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<style>
iframe {
width: 99vw;
}
.card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
width: 40%;
}
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
.container {
padding: 2px 16px;
}
</style>
<body>
<header>
<h1>groupie-tracker</h1>
</header>
<div>
{{range .}}
<div class="card">
<div class="container">
<h4><b>{{ .Name }}</b></h4>
<p>{{ .FirstAlbum }}</p>
</div>
</div>
{{end}}
</div>
</body>
</html>
答案1
得分: 0
处理模板的方法有几种。但是你肯定希望解析文件一次并将它们存储起来。
一种方法是创建一个全局变量,并将模板/模板存储在其中。
此外,你期望一个 Artist 结构的切片,对吗?所以你想要解组成一个切片,而不是一个单独的对象。如果你检查解组时的错误,你会看到这一点。
最后,CreationDate 字段存在问题。解组错误也会显示出来。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
)
type Response struct {
Artists string `json:"artists"`
Locations string `json:"locations"`
Dates string `json:"dates"`
Relation string `json:"relation"`
}
type Artist struct {
ID int `json:"id"`
Image string `json:"image"`
Name string `json:"name"`
Members []string `json:"members"`
CreationDate int `json:"creationDate"`
FirstAlbum string `json:"firstAlbum"`
}
var temp *template.Template
func main() {
temp = template.Must(template.ParseFiles("index.html"))
http.HandleFunc("/", formHandler) // to define the path/page, where we are going to use the mentioned function
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil { //to initialise our server on :8080
log.Fatal("HTTP status 500 - Internal server error: %s", err)
}
}
func formHandler(w http.ResponseWriter, r *http.Request) {
response, err := http.Get("https://groupietrackers.herokuapp.com/api")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var responseObject Response
json.Unmarshal(responseData, &responseObject)
artist_link, err := http.Get(responseObject.Artists)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
artistData, err := ioutil.ReadAll(artist_link.Body)
if err != nil {
log.Fatal(err)
}
var artists []Artist
err = json.Unmarshal(artistData, &artists)
if err != nil {
log.Fatal(err)
}
temp.Execute(w, artists)
}
英文:
There are a couple ways to handle templates. But you definitely want to parse your files once and store them.
One way is to create a global var and store the template/templates in there.
Also you expect a slice of Artist structs, correct? So you want to unmarshal into a slice, not a single object. If you check the error on unmarshal, you will see that.
Last, there is a problem with the CreationDate field. Unmarshal error shows that too.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
)
type Response struct {
Artists string `json:"artists"`
Locations string `json:"locations"`
Dates string `json:"dates"`
Relation string `json:"relation"`
}
type Artist struct {
ID int `json:"id"`
Image string `json:"image"`
Name string `json:"name"`
Members []string `json:"members"`
CreationDate int `json:"creationDate"`
FirstAlbum string `json:"firstAlbum"`
}
var temp *template.Template
func main() {
temp = template.Must(template.ParseFiles("index.html"))
http.HandleFunc("/", formHandler) // to define the path/page, where we are going to use the mentioned function
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil { //to initialise our server on :8080
log.Fatal("HTTP status 500 - Internal server error: %s", err)
}
}
func formHandler(w http.ResponseWriter, r *http.Request) {
response, err := http.Get("https://groupietrackers.herokuapp.com/api")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var responseObject Response
json.Unmarshal(responseData, &responseObject)
artist_link, err := http.Get(responseObject.Artists)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
artistData, err := ioutil.ReadAll(artist_link.Body)
if err != nil {
log.Fatal(err)
}
var artists []Artist
err = json.Unmarshal(artistData, &artists)
if err != nil {
log.Fatal(err)
}
temp.Execute(w, artists)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论