将未分组的 JSON 数据连接至HTML页面的 Go 语言实现方法
在 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知识!
更新应用程序的方法在运行时
- 上一篇
- 更新应用程序的方法在运行时
- 下一篇
- JavaScript读取操作的正确使用方法
-
- Golang · Go问答 | 1天前 |
- Go t.Cleanup注册后测试提前Fatal的资源释放边界
- 399浏览 收藏
-
- Golang · Go问答 | 1天前 |
- Go testing t.Parallel与t.Setenv冲突时的组织方式
- 265浏览 收藏
-
- Golang · Go问答 | 1天前 | 并发 · go · sync.Mutex go vet copylocks
- Go mutex复制后锁状态失效的代码审查要点
- 431浏览 收藏
-
- Golang · Go问答 | 1天前 | 并发 · go · 排查 · 数据竞争 Go并发 sync/atomic
- Go atomic值混用普通读写引起数据竞争的修复清单
- 289浏览 收藏
-
- Golang · Go问答 | 1天前 |
- Go sync.Pool取回旧对象状态时的重置排查
- 327浏览 收藏
-
- Golang · Go问答 | 1天前 |
- Go copy返回长度小于预期时的切片容量分析
- 246浏览 收藏
-
- Golang · Go问答 | 1天前 | 排序 · go ·
- Go sort.Slice比较器不满足严格弱序时的异常表现
- 404浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 198次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 254次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 209次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 193次使用
-
- CMMLU
- 深入了解CMMLU中文评估基准,涵盖67个学科主题,提供数据集下载、Zero-shot/Five-shot评估方法及排行榜,助力优化中文语言模型性能。
- 184次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go sql.Tx提交成功前读取结果导致事务边界混乱的修复方法
- 2026-09-20 501浏览
-
- Go select 用 time.After 做超时有什么资源代价
- 2026-09-10 501浏览
-
- Go 取 range 变量地址为什么得到重复指针
- 2026-09-07 501浏览

