使用 rhomobile 解析 json
我正在尝试使用 rhonobile 将 JSON 结构解析到数据库中,我的代码如下;
def GetTopGainers
Rho::AsyncHttp.get(
:url => 'http://www.xyz.com/xyz',
:callback => (url_for :action => :httpget_callback),
:callback_param => "" )
redirect :action => :viewhttp
end
def httpget_callback
$httpresult = @params['body'][0].inspect
@params['body'].each do |dailygainers|
mygainer = Topgainers.new
mygainer.gainers = Topgainers["dailygainers"]["gainers"]
mygainer.date = Topgainers["dailygainers"]["date"]
mygainer.open = Topgainers["dailygainers"]["Open"]
mygainer.close = Topgainers["dailygainers"]["Close"]
mygainer.save
end
WebView.refresh
end
问题是数据库中没有保存任何内容。
有什么想法吗?
i am trying to parse a JSON structure into a database using rhonobile and my code is below;
def GetTopGainers
Rho::AsyncHttp.get(
:url => 'http://www.xyz.com/xyz',
:callback => (url_for :action => :httpget_callback),
:callback_param => "" )
redirect :action => :viewhttp
end
def httpget_callback
$httpresult = @params['body'][0].inspect
@params['body'].each do |dailygainers|
mygainer = Topgainers.new
mygainer.gainers = Topgainers["dailygainers"]["gainers"]
mygainer.date = Topgainers["dailygainers"]["date"]
mygainer.open = Topgainers["dailygainers"]["Open"]
mygainer.close = Topgainers["dailygainers"]["Close"]
mygainer.save
end
WebView.refresh
end
The problem is that nothing is bei g saved to the database.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 dailygainers 是 do 循环的参数。您需要像这样实现它
Since dailygainers is the parameter to the do loop.You need to implement it like this
如果您的 Web 服务未设置正确的标头,您可能会返回字符串数据而不是 json 数据。您可以使用 JSON.parse(@params['body']) 将字符串转换为 json。
附加 rholog.txt 将有助于查看是否是这种情况。
If your web service is not setting the proper headers, you may be getting string data back instead of json data. You would use JSON.parse(@params['body']) to convert the string to json.
Attaching the rholog.txt would help to see if this is the case.
尝试查看日志并激活日志的跟踪模式,单击右键并选择选项->级别=跟踪。
会有一些重要的调试信息。如果没有,请尝试将完整的日志放在这里,以便我们可以更好地帮助您。
Try to view the log and activate the log's trace mode, clicking with right button on it and choose options->Level=Trace.
There would have some important debugging informations. If not, try to put here the complete log, so we can help you better.