3
我有一个哈希被贴到我的Rails应用程序看起来像这样:无法将HashWithIndifferentAccess转换为字符串?
{"signed_request"=>"...",
"buyer"=>"1064897036",
"receiver"=>"1064897036",
"order_id"=>"224539980951036",
"method"=>"payments_get_items",
"test_mode"=>"1",
"order_info"=>{"description"=>"None",
"image_url"=>"",
"price"=>"1",
"product_url"=>"",
"title"=>"Premium Membership"}}
我借用了一些代码来分析这一点,并发布适当的响应。首先,我解析已签名的请求,该请求工作正常,然后构建订单信息并将其发回服务器。这就是麻烦的开始,似乎:
...
elsif method == 'payments_get_items'
order_info = params[:order_info]
item = JSON.parse(order_info)
item['price'] = item['price'].to_i
# for url fields, if not prefixed by http://, prefix them
url_key = [ 'product_url', 'image_url' ]
url_key.each do |key|
if item[key][0..6] != 'http://'
item[key] = "http://#{item[key]}"
end
end
# if payload['test_mode']
if request.params['test_mode']
update_keys = ['title', 'description']
update_keys.each do |key|
item[key] = '[Test Mode] ' + item[key]
end
end
data['content'] = [item]
end
data['method'] = method
render :json => data
end
我得到这个错误:
can't convert ActiveSupport::HashWithIndifferentAccess into String
它是由这一行造成的:
item = JSON.parse(order_info)
我不知道这意味着什么。另一种方法,我猜...我可以让服务器传递一个项目号码,然后我可以查询数据库,并以JSON自己的方式提取信息,而不是试图解析哈希,但我想如果可以的话,做这个工作。有什么想法吗?