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 foremost
into the template func web page
?
bundle foremost
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"internet/http"
"os"
)
func foremost() {
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": "Residence"}`
And the consequence 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>Residence</li>
<li id=1>Residence</li>
</ul>
</physique>
When I attempt to go the JSON information into func web page(jsondata)
…
...
web page(string(physique))
}
func web page(jsondata) {
....
…I get some errors
./foremost.go:23:11: undefined: jsondata
./foremost.go:28:34: undefined: jsondata
And if I add string after jsondata
web page(string(physique))
}
func web page(jsondata string) {
I bought this errors:
can’t unmarshal array into Go worth of sort map[string]interface {}
Hello Sibert,
Are you able to ship the physique as an []byte?
On this approach you don’t must convert the info twice.
bundle foremost
import (
"encoding/json"
"fmt"
"html/template"
"io"
"internet/http"
"os"
)
func foremost() {
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 sort map[string]interface {}
JSON is far of a normal each my API and overseas API. So I suppose I’ve to discover ways to take care of JSON. How do I convert the info twice?
Here’s a play with out API. Similar error as at all times:
bundle foremost
import (
"encoding/json"
"html/template"
"os"
)
func foremost() {
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 sort map[string]interface {}
You possibly can unmarshal a JSON array right into a Go []interface
, or you possibly can unmarshal a JSON object right into a Go map[string]interface{}
. Higher can be to make use of extra particular Go sorts 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’t unmarshal array into Go worth of sort map[string]interface {}
Don’t work so far as I can see?
After some Googling, I discovered one resolution:
var m []map[string]interface{}
The error messages says what the issue is. You are attempting to unmarshal a json array right into a map. I instructed you and confirmed you that you would be able to unmarshal a json array right into a slice. In case your json is an object, then you possibly can unmarshal it right into a map. It is advisable have some understanding of the construction of the json.
Sure, []map[string]interface{}
is a bit more particular than []interface{}
, and that ought to make for easier code accessing the info.
I do perceive the construction of JSON. However I’m misplaced relating to Go semantics and syntax for learn and write JSON in Go.
For an instance this was complicated to me:m := []map[string]interface{}
//does NOT work
howevervar m []map[string]interface{}
//does work
m := []map[string]interface{}
assigns the a sort to the variable m as a result of the right-hand facet is a kind of a slice of maps, not a worth.var m []map[string]interface{}
declares the variable m to be of a kind (of slice of maps). It’s implicitly initialized to the zero-value of the sort.:=
is a shorthand to declare a variable and initialize it. The variable’s worth might be set to the worth of the right-hand facet, and it’s sort might be inferred from the kind of the right-hand facet. A Tour of Go
When utilizing json.Unmarshal
, it’s pointless to initialize the variable as a result of Unmarshal
will set the variable`s worth.
2 Likes