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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)