登录
首页 >  Golang >  Go问答

将未分组的 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 响应。您可以参考以下示例开始。

57188​​8686339

好了,本文到此结束,带大家了解了《将未分组的 JSON 数据连接至HTML页面的 Go 语言实现方法》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>