【发布时间】:2014-01-04 12:17:27
【问题描述】:
我有一个向我的 Rails 后端发出请求的移动应用程序。
后端JSON以如下方式返回信息:
def get_data
respond_to do |format|
format.json{
data = Form.select('column1, column2').where(field:'Rhodes')
render :json => data
}
end
end
在我的应用程序 (Rhodes) 中,我使用 Rho::AsyncHttp.post 并将数据发送到我的 webview 索引:
response = Rho::AsyncHttp.post(
:url => 'http://xyx.xyx.x.yx:3000/data/get_data.json',
:headers => {"Content-Type" => "application/json; charset=utf-8", "Accept" => "application/json"}
)
WebView.navigate( url_for :action => :index , :query => { :form => response['body'] })
现在,当我在 web 视图中检查变量 form 时,我会得到以下结构的数据:
<%= @form.inspect %>
"[{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"},{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"},{\"campo\"=>\"nombre\",\"tipo\"=>\"textfield\"}]"
所以,我需要在表格中显示这些数据并且我想要循环这个。我试过each:
<h2>Data</h2>
<% @form.each do |x| %>
<div><%= x.tipo %></div>
<% end %>
但我得到了错误:undefined method each for #.
所以,我尝试将其转换为 JSON 对象:
data = Rho::JSON.parse(@form)
但是,我收到以下错误:
JSON error code: 10; Ofset: 9
Trace:
(eval):36:in ´parse´
(eval):36:in '´
lib/rho/render.rb:175:in ´eval_compiled_file´
lib/rho/render.rb:175:in ´render´ [...]
我将 Ruby 1.9.3 与 Rhodes 3.5.1.12 一起使用
更新
我解决了函数eval 的问题。现在我可以循环字符串了。
但是当我尝试打印变量时。这些不显示。
<% data = eval(@form) %>
<% data.each do |hash| %>
<ul>
<li><%= puts hash['campo'] %></li> #only display the bullets but no the data
<li><%= puts hash['tipo'] %></li> #only display the bullets but no the data
</ul>
<br />
<% end %>
【问题讨论】: