【问题标题】:Ruby on Rails and JSON parser from URL来自 URL 的 Ruby on Rails 和 JSON 解析器
【发布时间】:2013-09-02 22:54:08
【问题描述】:

我使用 'gem json' 并且需要从一些 url 加载 JSON 数据,例如:

"http://locallhost:3000/qwerty/give_json.json"

{"one":"Omg","two":125,"three":"Hu"}

我有 Rails 应用程序

class QwertyController < ApplicationController
    require 'json'

    def get_json
        source = "http://localhost:3000/qwerty/give_json.json"
        @data = JSON.parse(JSON.load(source))
    end
end

我得到错误

JSON::ParserError in QwertyController#get_json
795: unexpected token at 'http://localhost:3000/qwerty/give_json.json'

在字符串中:@data = JSON.parse(JSON.load(source))

这是怎么回事?如何获取 JSON 数据并对其进行解析?我试试@data["one"] ...

【问题讨论】:

  • 看起来您的源中的 JSON 无效
  • 不,不是。我创建了一个 Rails 应用程序,它在端口 3000 (localhost:3000/qwerty/give_json.json) 上生成 JSON 答案。第二个应用程序,它尝试在端口 3001 (localhost:3001/qwerty/get_json) 上获取 JSON 数据
  • 您使用的是什么版本的 ruby​​?您不需要在较新版本的 rails 和 ruby​​ 中使用 json...
  • alexkd@Active-pc:~$ ruby​​ -v ruby​​ 1.9.3p448 (2013-06-27 修订版 41675) [i686-linux] alexkd@Active-pc:~$ rails -v Rails 4.0 .0
  • 是的,您确实有无效的 JSON。您在 URL 字符串上调用 JSON.load,而不是 JSON 对象。那是无效的 JSON。 JSON.load 不会从 url 读取数据,你只是在传递字符串

标签: ruby-on-rails ruby json parsing


【解决方案1】:

JSON.load 根据文档获取 StringIO 对象的源

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-load

[17] pry(main)> {hello: "World"}.to_json
=> "{\"hello\":\"World\"}"
[18] pry(main)> JSON.load(_)
=> {"hello"=>"World"}

你给它一个字符串,它是一个 URL,这就是你收到错误的原因。您可以使用open-uri 从 URL 中获取数据,然后像这样被 JSON 解析...

[22] pry(main)> require 'open-uri'
=> false
[23] pry(main)> JSON.load(open("https://api.github.com"))
=> {"current_user_url"=>"https://api.github.com/user",
 "authorizations_url"=>"https://api.github.com/authorizations",
 "emails_url"=>"https://api.github.com/user/emails",
 "emojis_url"=>"https://api.github.com/emojis",
 "events_url"=>"https://api.github.com/events",
 "feeds_url"=>"https://api.github.com/feeds",
 "following_url"=>"https://api.github.com/user/following{/target}",
 "gists_url"=>"https://api.github.com/gists{/gist_id}",
 "hub_url"=>"https://api.github.com/hub"}

注意

open 返回一个 StringIO 对象,该对象响应 read 返回 JSON 数据。 JSON.load 将数据转换为要使用的哈希。

要解析 JSON 字符串,您可以使用 JSON.loadJSON.parse

【讨论】:

    【解决方案2】:

    你可以像下面这样使用 net/http 库:

       require 'net/http'
       source = 'http://localhost:3000/qwerty/give_json.json'
       resp = Net::HTTP.get_response(URI.parse(source))
       data = resp.body
       result = JSON.parse(data)
    

    或者gem http派对:

    require 'httparty'
    
    response = HTTParty.get('http://localhost:3000/qwerty/give_json.json')
    json = JSON.parse(response.body)
    

    【讨论】:

      【解决方案3】:

      默认情况下,httparty 已在 httparty.rb 中包含 JSON 库。
      这意味着对require 'json' 的调用是不必要的。

      感谢您提供这些示例!

      【讨论】:

        【解决方案4】:

        您可以使用 JSON 和 net/http lib.. 如下:

        require 'net/http'
        require 'json'
        
        url = "https://api.url/"
        uri = URI(url)
        response = Net::HTTP.get(uri)
        data = JSON.parse(response)
        objs.each do |data|
          title = data["title"]
        

        【讨论】:

          猜你喜欢
          • 2013-03-23
          • 1970-01-01
          • 2019-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-30
          • 1970-01-01
          • 2010-12-22
          相关资源
          最近更新 更多