为什么我的订单数据没有保存在我的json文件中 [英] Why isn't my order data saved in my json file

查看:126
本文介绍了为什么我的订单数据没有保存在我的json文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将数据发送到json文件,它将发送客户的所有数据,而不发送我的订单数据.我认为我的代码无法正确转换类型,但是找不到位置.我认为它位于Pizzamap中的某个位置,但我找不到错误.

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.

处理程序:

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, "", "\t")
if err != nil {
    panic(err)
}

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

}

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>

结构:

type PizzaOrder struct {
Naam   string
Price  float64
Aantal int

}

这就是我的json文件中显示的内容

this is what it shows in my json file

json:

"0": {
    "Naam": "Lucas",
    "Tussenvoegsel": "de",
    "Achternaam": "Groot",
    "Adres": "Margrietlaan 37",
    "Postcode": "6713PL",
    "Telefoonnummer": "03958321",
    "Email": "k@gmail.com",
    "Status": "Je bestelling wordt bereid",
    "Totaalprijs": 0,
    "Order": {
        "0": {
            "Naam": "salami",
            "Price": 0,
            "Aantal": 2
        }
    }
}

推荐答案

无法将字符串解组到Go结构字段PizzaOrder中.float64类型的价格

cannot unmarshal string into Go struct field PizzaOrder.Price of type float64

这清楚表明一个字符串正试图被解码为浮点数

This makes it clear that a string is trying to be decoded into a float

查看您的类型定义:

type PizzaOrder struct {
    Naam   string
    Price  float64 // <-- expects float
    Aantal int     // <-- expects int
}

您的输入实际上不会被解组,因为PriceAantal分别是string而不是float64int

Your input will really not be unmarshal'ed because Price and Aantal are string instead of float64 and int respectively

"Order": {
    "0": {
        "Naam": "",
        "Price": "", // <-- string
        "Aantal": "" // <-- string
    }
}

应该是

"Order": {
    "0": {
        "Naam": "",
        "Price": 0,
        "Aantal": 0
    }
}

不应该

Pizzabesteld[index] = PizzaOrder{

Pizzamap[index] = PizzaOrder{

?您正在尝试输出Pizzamap,但实际上它是一个空地图.

? You are trying to output Pizzamap but it is actually an empty map.

这篇关于为什么我的订单数据没有保存在我的json文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆