使用 rhomobile 解析 json

发布于 2024-10-07 16:48:17 字数 745 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末的微笑 2024-10-14 16:48:17

由于 dailygainers 是 do 循环的参数。您需要像这样实现它

mygainer.gainers = dailygainers ["dailygainers"]["gainers"]
mygainer.date = dailygainers ["dailygainers"]["date"]
mygainer.open = dailygainers ["dailygainers"]["Open"]
mygainer.close = dailygainers ["dailygainers"]["Close"]
mygainer.save

Since dailygainers is the parameter to the do loop.You need to implement it like this

mygainer.gainers = dailygainers ["dailygainers"]["gainers"]
mygainer.date = dailygainers ["dailygainers"]["date"]
mygainer.open = dailygainers ["dailygainers"]["Open"]
mygainer.close = dailygainers ["dailygainers"]["Close"]
mygainer.save
若有似无的小暗淡 2024-10-14 16:48:17

如果您的 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.

待天淡蓝洁白时 2024-10-14 16:48:17

尝试查看日志并激活日志的跟踪模式,单击右键并选择选项->级别=跟踪。
会有一些重要的调试信息。如果没有,请尝试将完整的日志放在这里,以便我们可以更好地帮助您。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文