dsdv76767671 2016-07-14 09:34
浏览 245

Beego:如何解析对象url参数?

The URL I am receiving looks like this:

/controller/action?columns[0][data]=foo&columns[1][data]=bar&columns[2][data]=bar&columns[3][data]=bar&columns[4][data]=bar

So I have an array of objects. How can I parse this into a slice of structs?

What I have in my controller action is this:

func (this *MyController) Foo() {

  type column struct{
    Data string
  }

  columns := []column{}

  if err := this.Ctx.Input.Bind(&columns, "columns"); err != nil {
    log.Fatal(err)
  } else {
    log.Println("OK, got:", columns)
  }

}

And the output of this is:

OK, got: [{} {} {} {} {} {}]

Bind successfully detects there are five columns but fails to map the data field. I experimented with this and believe there are two problems:

The bind seems to only support the exact syntax shown in the documentation:

?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie

user.Name=astaxie works while user[Name]=astaxie does not.

The 2nd problem is, the lower case of data. My experiments have shown user.Name works while user.name does not.

So the data bind method expects me to get the parameters in the form columns[0].Data=foo while I have columns[0][data]=foo

The first one indeed works. But I have no control over the URL, I have to accept it as it is, with lower case data in square brackets. The request is generated by DataTables.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法