dongle7553 2018-07-30 10:41
浏览 64

重复formData to Go结构

I am submitting a repeating form, where the resulting formData is parsed as:

"Name":  {"John", "Jake"},
"Phone": {"999-999-999", "12312-123-123"}, 

However, I want to structure to be

{ Name: "John", Phone: "999-999-999" }, 
{ Name: "Jake", Phone: "12312-123-123" }.

I'm told gorilla/schema, is a good fit, but I tried it below and it produces an empty slice. Is there something I am missing?

package main

import (
        "fmt"

        "github.com/gorilla/schema"
)

type Person struct {
        Name  string
        Phone string
}

func main() {
        values := map[string][]string{
                "Name":  {"John", "Jake"},
                "Phone": {"999-999-999", "12312-123-123"},
        }
        var persons []Person
        decoder := schema.NewDecoder()
        decoder.Decode(persons, values)
        fmt.Println(persons)
}
  • 写回答

1条回答 默认 最新

  • doulong1987 2018-07-30 10:58
    关注

    In your provided data of json each key contains two values. That's the reason behind empty slices. Use slice of string in your struct to unmarshal the value.

    package main
    
    import (
        "fmt"
    
        "github.com/gorilla/schema"
    )
    
    type Person struct {
        Name  []string // this should be a slice since the key contains multiple values
        Phone []string
    }
    
    type Person2 struct {
        Name  string
        Phone string
    }
    
    func main() {
        values := map[string][]string{
            "Name":  {"John", "Jake"},
            "Phone": {"999-999-999", "12312-123-123"},
        }
        person := new(Person)
        decoder := schema.NewDecoder()
        decoder.Decode(person, values)
        fmt.Println(person)
    }
    

    Output:

    &{[John Jake] [999-999-999 12312-123-123]}
    

    For required structure

    { Name: "John", Phone: "999-999-999" }, 
    { Name: "Jake", Phone: "12312-123-123" }
    

    Edited:

    Process the data coming from form to change the format of your structure.

    type Person2 struct {
        Name  string
        Phone string
    }
    
    func processData(person *Person) {
        var result []Person2
        var person2 Person2
        for i := 0; i < len(person.Name); i++ {
            person2.Name = person.Name[i]
            person2.Phone = person.Phone[i]
            result = append(result, person2)
        }
        fmt.Printf("%#v
    ", result)
    }
    

    Output:

    []stack.Person2{stack.Person2{Name:"John", Phone:"999-999-999"}, stack.Person2{Name:"Jake", Phone:"12312-123-123"}}
    

    Playground Example to process the data

    As @Adrain suggested it is better to use some kind of indexing with the field names for the form. It is also provided in gorilla/schema package to name the values with indexes for saving multiple records.

    <form>
        <input type="text" name="Name">
        <input type="text" name="Phones.0.Label">
        <input type="text" name="Phones.0.Number">
        <input type="text" name="Phones.1.Label">
        <input type="text" name="Phones.1.Number">
        <input type="text" name="Phones.2.Label">
        <input type="text" name="Phones.2.Number">
    </form>
    

    Above form can be parsed to below struct which contains a slice of Phone:

    type Person struct {
        Name   string
        Phones []Phone
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?