如何将Ruby on Rails中的HTTP请求参数转换为哈希?
- 内容介绍
- 文章标签
- 相关推荐
本文共计395个文字,预计阅读时间需要2分钟。
从字符串中提取数组的方法如下:
pythonimport json
字符串input_str=http_params=%22created_end_date%3D2013-02-28created_start_date%3D2013-01-01page_size%3D50offset%3D0order_id%3D0D1108211501118%0D%0A0D11108211501118%0D%0Ac%0D%0AD%0D%0ADK212071409743%0D%0AKK30109110100%0D%0AKK3%22
将字符串分割成字典键值对pairs=input_str.split(created_end_date%3D)[1].split(created_start_date%3D)[0].split(page_size%3D)[0].split(offset%3D)[0].split(order_id%3D)[0].split(%22%0D%0A)
构建字典params_dict={}for pair in pairs: if pair: key, value=pair.split(%3D) params_dict[key]=value
转换字典为JSON数组params_list=json.dumps([params_dict])
输出结果print(params_list)
我可以从字符串中获取一个数组http_params="created_end_date=2013-02-28&created_start_date=2013-01-01&page_size=50&offset=0&order_id=0D1108211501118%0D%0A0D11108211501118%0D%0Ac%0D%0AD%0D%0ADK212071409743%0D%0AKK30109110100%0D%0AKK30111140300%0D%0AKK30111140400%0D%0AKK30115120100%0D%0AKK30115150100&page_number=1"
所以我做了myarray = http_params.split(“&”):
myarray=["created_end_date=2013-02-28", "created_start_date=2013-01-01", "page_size=50", "offset=0", "order_id=0D1108211501118%0D%0A0D11108211501118%0D%0Ac%0D%0AD%0D%0ADK212071409743%0D%0AKK30109110100%0D%0AKK30111140300%0D%0AKK30111140400%0D%0AKK30115120100%0D%0AKK30115150100", "page_number=1"]
我需要将其转换为哈希myhash,以便我可以为myhash.to_json进行Rest Client调用.基本上它应该是关键的,价值对,如:
{:created_end_date=>"2013-02-28",:created_start_date=>"2013-01-01"....}
我知道逆操作可以像这样完成:
http_params = myhash.map{|k,v| "#{k}=#{v}"}.join('&')
但是我无法为此提出整洁的代码.
我应该采取什么样的最佳方式?
require 'cgi' hash = CGI::parse http_params
或者您可以使用:
hash = Rack::Utils.parse_nested_query http_params
哪个不会将值作为数组返回.
本文共计395个文字,预计阅读时间需要2分钟。
从字符串中提取数组的方法如下:
pythonimport json
字符串input_str=http_params=%22created_end_date%3D2013-02-28created_start_date%3D2013-01-01page_size%3D50offset%3D0order_id%3D0D1108211501118%0D%0A0D11108211501118%0D%0Ac%0D%0AD%0D%0ADK212071409743%0D%0AKK30109110100%0D%0AKK3%22
将字符串分割成字典键值对pairs=input_str.split(created_end_date%3D)[1].split(created_start_date%3D)[0].split(page_size%3D)[0].split(offset%3D)[0].split(order_id%3D)[0].split(%22%0D%0A)
构建字典params_dict={}for pair in pairs: if pair: key, value=pair.split(%3D) params_dict[key]=value
转换字典为JSON数组params_list=json.dumps([params_dict])
输出结果print(params_list)
我可以从字符串中获取一个数组http_params="created_end_date=2013-02-28&created_start_date=2013-01-01&page_size=50&offset=0&order_id=0D1108211501118%0D%0A0D11108211501118%0D%0Ac%0D%0AD%0D%0ADK212071409743%0D%0AKK30109110100%0D%0AKK30111140300%0D%0AKK30111140400%0D%0AKK30115120100%0D%0AKK30115150100&page_number=1"
所以我做了myarray = http_params.split(“&”):
myarray=["created_end_date=2013-02-28", "created_start_date=2013-01-01", "page_size=50", "offset=0", "order_id=0D1108211501118%0D%0A0D11108211501118%0D%0Ac%0D%0AD%0D%0ADK212071409743%0D%0AKK30109110100%0D%0AKK30111140300%0D%0AKK30111140400%0D%0AKK30115120100%0D%0AKK30115150100", "page_number=1"]
我需要将其转换为哈希myhash,以便我可以为myhash.to_json进行Rest Client调用.基本上它应该是关键的,价值对,如:
{:created_end_date=>"2013-02-28",:created_start_date=>"2013-01-01"....}
我知道逆操作可以像这样完成:
http_params = myhash.map{|k,v| "#{k}=#{v}"}.join('&')
但是我无法为此提出整洁的代码.
我应该采取什么样的最佳方式?
require 'cgi' hash = CGI::parse http_params
或者您可以使用:
hash = Rack::Utils.parse_nested_query http_params
哪个不会将值作为数组返回.

