convert ruby hash to URL query string … without those square brackets

后端 未结 5 1793
遇见更好的自我
遇见更好的自我 2021-02-13 16:46

In Python, I can do this:

>>> import urlparse, urllib
>>> q = urlparse.parse_qsl(\"a=b&a=c&d=e\")
>>> urllib.urlencode(q)
\'a=         


        
5条回答
  •  自闭症患者
    2021-02-13 17:26

    Here's a quick function to turn your hash into query parameters:

    require 'uri'
    def hash_to_query(hash)
      return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
    end
    

提交回复
热议问题