dongshi9526 2016-07-02 20:59
浏览 40

将多维数组格式的查询字符串解析为实际数组

I am trying to create an extensive search url with this format ?filter[1][field]=brandId&filter[1][operand]=>&filter[1][values][]=firstvalue but I cant seem to figure out a way to convert this (string) to an actual array (map/slice or anything i can loop on).

i have read a lot of documentation and searched on google but cannot find a good way to do this.

  • 写回答

1条回答 默认 最新

  • dongpao9437 2016-07-03 01:16
    关注

    You could range on the parsed query like so if your filter is going to be an unknown length:

    https://play.golang.org/p/NSQ7bnJXef

        v, err := url.ParseQuery("filter[1][field]=brandid&filter[1][operand]=>&filter[1][values][]=firstvalue&filter[2][field]=brandid&filter[2][operand]=>&filter[2][values][]=firstvalue")
        if err != nil {
            fmt.Println(err)
            return
        }
        for id, thing := range v {
            switch {
            case strings.Contains(id, "field"):
                log.Printf("Field Value: %v", thing)
            case strings.Contains(id, "values"):
                log.Printf("Values Value: %v", thing)
            case strings.Contains(id, "operand"):
                log.Printf("Operand Value: %v", thing)
            }
        }
    

    But IMO, there has to be a better way than doing the query you're doing now.

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程