Friday, March 24, 2023
HomeGolangJSON into Go html template? - Getting Assist

JSON into Go html template? – Getting Assist


I attempt to retailer the menu in a database and fill a Go html template with this. I’ve managed to fetch the JSON and the filling of Go html template. Unbiased of one another. However how do I switch the JSON from the func important into the template func web page?

package deal important

import (
    "encoding/json"
    "fmt"
    "html/template"
    "io/ioutil"
    "web/http"
    "os"
)

func important() {
    resp, err := http.Get("https://api3.go4webdev.org/menu/all")
    if err != nil {
        fmt.Println("No response")
    }
    defer resp.Physique.Shut()
    physique, err := ioutil.ReadAll(resp.Physique)
    fmt.Println(string(physique)) // how go this to "func web page" and populate the menu?
    web page()
}

func web page() {
    fmt.Println()
    t := template.Should(template.New("").Parse(templ))

    m := map[string]interface{}{}
    if err := json.Unmarshal([]byte(jsondata), &m); err != nil {
        panic(err)
    }

    if err := t.Execute(os.Stdout, m); err != nil {
        panic(err)
    }
}

const templ = `
<html><physique>
   <ul>
     <li id={{.menu_id}}>{{.menu_txt}}</li>
     <li id={{.menu_id}}>{{.menu_txt}}</li>
   </ul>
</physique></html>`

const jsondata = `{"menu_id":"1", "menu_txt": "Dwelling"}`

And the consequence can be this:

[{"menu_icn":"","menu_id":"code","menu_lnk":"/code","menu_main":"code","menu_sort":1,"menu_txt":"Code Philosophy"},{"menu_icn":"","menu_id":"home","menu_lnk":"/home","menu_main":"home","menu_sort":1,"menu_txt":"Navigation”}, etc…]

<html><physique>
   <ul>
     <li id=1>Dwelling</li>
     <li id=1>Dwelling</li>
   </ul>
</physique>

When I attempt to go the JSON knowledge into func web page(jsondata)

...
	web page(string(physique))
}

func web page(jsondata) {
....

…I get some errors

./important.go:23:11: undefined: jsondata
./important.go:28:34: undefined: jsondata

And if I add string after jsondata

	web page(string(physique))
}

func web page(jsondata string) {

I received this errors:

can’t unmarshal array into Go worth of kind map[string]interface {}


Hello Sibert,
Are you able to ship the physique as an []byte?
On this method you don’t must convert the info twice.

package deal important

import (
	"encoding/json"
	"fmt"
	"html/template"
	"io"
	"web/http"
	"os"
)

func important() {
	resp, err := http.Get("https://api3.go4webdev.org/menu/all")
	if err != nil {
		fmt.Println("No response")
	}
	defer resp.Physique.Shut()
	physique, err := io.ReadAll(resp.Physique)
	fmt.Println(string(physique)) // how go this to "func web page" and populate the menu?
	web page(physique)
}

func web page(jsondata []byte) {
	fmt.Println()
	t := template.Should(template.New("").Parse(templ))

	m := map[string]interface{}{}
	if err := json.Unmarshal([]byte(jsondata), &m); err != nil {
		panic(err)
	}

	if err := t.Execute(os.Stdout, m); err != nil {
		panic(err)
	}
}

const templ = `
<html><physique>
   <ul>
     <li id={{.menu_id}}>{{.menu_txt}}</li>
     <li id={{.menu_id}}>{{.menu_txt}}</li>
   </ul>
</physique></html>`

Your code gave this error:

panic: json: can't unmarshal array into Go worth of kind map[string]interface {}

JSON is way of a regular each my API and overseas API. So I assume I’ve to learn to cope with JSON. How do I convert the info twice?

Here’s a play with out API. Similar error as at all times:

package deal important

import (
	"encoding/json"
	"html/template"
	"os"
)

func important() {
	t := template.Should(template.New("").Parse(templ))

	m := map[string]interface{}{}
	if err := json.Unmarshal([]byte(jsondata), &m); err != nil {
		panic(err)
	}

	if err := t.Execute(os.Stdout, m); err != nil {
		panic(err)
	}
}

const templ = `
<html><physique>
{{ vary . }}
   <li id={{.menu_id}}> {{ .menu_txt }}</li>
{{ finish }}
</physique></html>`

const jsondata = `[{"menu_id":"1", "menu_txt": "Home"},{"menu_id":"2", "menu_txt": "Prefs"}]`

panic: json: can’t unmarshal array into Go worth of kind map[string]interface {}

You possibly can unmarshal a JSON array right into a Go []interface, or you may unmarshal a JSON object right into a Go map[string]interface{}. Higher can be to make use of extra particular Go varieties that match the schema of your JSON.

	m := map[string]interface{}{}
	if err := json.Unmarshal([]interface(jsondata), &m); err != nil {
		panic(err)
	}

syntax error: sudden (, anticipating {

Or how do you imply?

RELATED ARTICLES

Most Popular

Recent Comments