Friday, March 10, 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. Impartial of one another. However how do I switch the JSON from the func important into the template func web page?

bundle 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 cross 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 outcome might 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 cross 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 obtained this errors:

can not 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 should convert the information twice.

bundle 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 cross 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 not unmarshal array into Go worth of kind map[string]interface {}

JSON is way of a typical each my API and international API. So I suppose I’ve to learn to cope with JSON. How do I convert the information twice?

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

bundle 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 not unmarshal array into Go worth of kind map[string]interface {}

You may 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?

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

Don’t work so far as I can see?

After some Googling, I discovered one answer:

var m []map[string]interface{}

RELATED ARTICLES

Most Popular

Recent Comments