将未分组的 JSON 数据连接至HTML页面的 Go 语言实现方法
来源:stackoverflow
时间:2024-03-22 22:48:32 375浏览 收藏
在 Go 语言中,将未分组的 JSON 数据连接到 HTML 页面需要使用 html template 包。首先,将 JSON 数据解组到一个结构体中,然后使用该结构体作为模板数据的来源。在 HTML 页面中,使用模板标签将数据插入到适当的位置。最后,使用 RenderTemplate 函数将填充了数据的模板渲染到 HTTP 响应中。本例中,JSON 数据包含一组具有 id、description、displayname、status 和 type 属性的结果。
我已经解组了 json,并且可以直接从registry.go 文件打印数据。我不明白(而且我是 golang 和后端开发的新手)是如何将该数据连接到我的 html 页面。如果有人能向我解释如何做到这一点,我将非常感激。
我看过一些示例,但它们都在一个文件中,我想将 json、html 和 go 页面分开。 json 是直接从 api 中提取的,并且将有多个 html 页面使用该数据。
这是一个示例,与我正在使用的 json 非常接近,它是从 api 中提取的:
{ "count": 4, "next": null, "previous": null, "results": [ { "id": 1, "description": "some text", "displayname": "some more text", "status": { "status": "up", "lastupdate": "2020-07-21t12:42:24.968647z" }, "type": "test" }, { "id": 2, "description": "some text", "displayname": "some more text", "status": { "status": "up", "lastupdate": "2020-07-21t12:42:24.968647z" }, "type": "test" }, { "id": 3, "description": "some text", "displayname": "some more text", "status": { "status": "up", "lastupdate": "2020-07-21t12:42:24.968647z" }, "type": "test" }, { "id": 4, "description": "some text", "displayname": "some more text", "status": { "status": "up", "lastupdate": "2020-07-21t12:42:24.968647z" }, "type": "test" } ] }
这是我正在使用的registry.go 页面。我有一个单独的 main.go 页面,因为我有其他已创建的页面。我试图将这一部分分开,但如果我不能,请告诉我。
package main import ( "encoding/json" "fmt" "io/ioutil" "os" ) // results struct which contains // an array of results type results struct { count int `json:"count"` results []result `json:"results"` } // results struct which contains the id and description type result struct { id int `json:"id"` description string `json:"description"` displayname string `json:"displayname"` status status `json:"status"` type string `json:"type"` } // status struct type result struct { status string `json:"status"` lastupdated string `json:"lastupdated"` } func main() { // open the jsonfile jsonfile, err := os.open("test.json") // if os.open returns an error then handle it if err != nil { fmt.println(err) } fmt.println("successfully opened test.json") // defer the closing of the jsonfile in order to parse it later on defer jsonfile.close() // read the opened xmlfile as a byte array. bytevalue, _ := ioutil.readall(jsonfile) // initialize the results array var results results // unmarshal the bytearray json.unmarshal(bytevalue, &results) // print the count fmt.println(results.count) // iterate through the results and print for i := 0; i < len(users.users); i++ { fmt.println("id: " + results.results[i].id) fmt.println("desctiption: " + results.results[i].description) } }
这是我创建的 html 页面 (test.html),仅显示正文部分:
{{range .}}{{.displayname}}
{{.id}}{{.description}}{{.type}}{{end}}
在 main.go 中我有以下参考文献:
//handler to display the page http.HandleFunc("/test", hndlerTest) //hndlerTest - the Testing Page func hndlerTest(w http.ResponseWriter, req *http.Request){ renderTemplate(w,"test.html", nil) }
解决方案
您可以使用 html template package 解析和渲染 html 文件,并将执行的模板写入您的 http 响应。您可以参考以下示例开始。
571888686339好了,本文到此结束,带大家了解了《将未分组的 JSON 数据连接至HTML页面的 Go 语言实现方法》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习