Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

go - Why isn't my order data saved in my json file

If I send data to my json file, it sends all the data of the customer, but not the data of my order. I think my code doesn't convert the types right, but I can't find the location. I think it's located somewhere in the pizzamap, but i can't find a error.

The Handler:

func bestellingHandler(writer http.ResponseWriter, request *http.Request) {
log.Println("Viewing bestellingen")
bestellingTemplate, _ := template.ParseFiles("./templates/Bestellingen.htm")

// laad de pizza's uit de data file

fmt.Println(request.Method)
fmt.Println(request.Form)
fmt.Println(request.PostFormValue("name"))

filepizza := "./data/pizzas.json"
Readpizza, err := ioutil.ReadFile(filepizza)

var Pizzaslice []Pizza

err = json.Unmarshal(Readpizza, &Pizzaslice)
if err != nil {
    panic(err)
}

Pizzamap := make(map[string]PizzaOrder)
var totaalprijs float64
for i := 0; i < (len(Pizzaslice)); i++ {

    index := strconv.Itoa(i)
    aantalpizzas, _ := strconv.Atoi(request.FormValue("aantal_" + Pizzaslice[i].Name))
    prijspizzas, _ := strconv.ParseFloat(request.FormValue("price"+Pizzaslice[i].Name), 64)
    if aantalpizzas != 0 {

        Pizzabesteld[index] = PizzaOrder{
            Naam:   request.FormValue("name" + Pizzaslice[i].Name),
            Price:  prijspizzas,
            Aantal: aantalpizzas,
        }

        fmt.Println(Pizzabesteld)
        totaalprijs += float64(aantalpizzas) * prijspizzas
    }
}

filebestelling := "./data/deck.json"
Readbestelling, err := ioutil.ReadFile(filebestelling)

var klantmap map[string]Klant

err = json.Unmarshal(Readbestelling, &klantmap)
if err != nil {
    panic(err)
}
index := strconv.Itoa(len(klantmap))
klantmap[index] = Klant{
    Naam:           request.FormValue("naam"),
    Tussenvoegsel:  request.FormValue("tussenv"),
    Achternaam:     request.FormValue("lname"),
    Adres:          request.FormValue("adress"),
    Telefoonnummer: request.FormValue("phone"),
    Postcode:       request.FormValue("zipcode"),
    Email:          request.FormValue("email"),
    Status:         "Je bestelling wordt bereid",
    Totaalprijs:    totaalprijs,
    Order:          Pizzamap,
}

Resultaat, err := json.MarshalIndent(klantmap, "", "	")
if err != nil {
    panic(err)
}

ioutil.WriteFile(filebestelling, Resultaat, 0644)
fmt.Sprintln(klantmap[index])
bestellingTemplate.Execute(writer, klantmap[index])

}

The HTML:

<fieldset>
               <legend>Je bestelling:</legend>
                <ul>

                    <li>
                        <p>Naam: {{ .Naam }}<br>
                        Tussenvoegsel: {{ .Tussenvoegsel }}<br>
                        Achternaam: {{ .Achternaam }}<br>
                        E-mail: {{ .Email }}<br>
                        Telefoonnummer: {{ .Telefoonnummer }}<br>
                        Status: {{ .Status }}<br>
                        <fieldset>
                            <ul>
                        {{range $key, $value := .Order}}
                        <li>
                         Pizzanaam: {{ $value.Naam }}  <br>
                         Prijs: &euro;{{ $value.Price | printf "%.2f" }}  <br>
                         Aantal: {{ $value.Aantal }} <br>
                        </li>
                        {{end}} 
                        </ul>
                        </fieldset>
                        </p>
                    </li>
                    <li>Totaal: &euro;{{ .totaalprijs | printf "%.2f" }}</li>


                </ul>
            </fieldset>

The Structures:

type PizzaOrder struct {
Naam   string
Price  float64
Aantal int

}

this is what it shows in my json file

The json:

"0": {
    "Naam": "Lucas",
    "Tussenvoegsel": "de",
    "Achternaam": "Groot",
    "Adres": "Margrietlaan 37",
    "Postcode": "6713PL",
    "Telefoonnummer": "03958321",
    "Email": "[email protected]",
    "Status": "Je bestelling wordt bereid",
    "Totaalprijs": 0,
    "Order": {
        "0": {
            "Naam": "salami",
            "Price": 0,
            "Aantal": 2
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...