来自 URL 的 Ruby on Rails 和 JSON 解析器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18581792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:39:05  来源:igfitidea点击:

Ruby on Rails and JSON parser from URL

ruby-on-railsrubyjsonparsing

提问by Alex Kurmaev

I use 'gem json' and need load JSON data from some url, for example:

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

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

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

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

I have rails app

我有 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

I get error

我得到错误

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

In string: @data = JSON.parse(JSON.load(source))

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

What is the matter? How can I get JSON data and parse it? I try @data["one"] ...

有什么事?如何获取 JSON 数据并解析它?我试试@data["one"] ...

回答by Leo Correa

JSON.loadtakes a source that is a Stringor IOobject according to the documentation

JSON.load根据文档采用一个StringIO对象的源

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

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"}

You're giving it a String that is a URL and that's why you're getting an error. You can use open-urito fetch the data from the URL to then be parsed by JSON like so...

你给它一个字符串,它是一个 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"}

NOTE

笔记

openreturns a StringIOobject which responds to readreturning the JSON data. JSON.load turns the data into a hash to be used.

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

To parse the JSON string you can either use JSON.loador JSON.parse

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

回答by Junior Joanis

You could use net/http library like below:

您可以使用如下所示的 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)

Or the gem http party:

或者 gem http 派对:

require 'httparty'

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

回答by carlosplusplus

By default, httpartyalready includes the JSON library within httparty.rb.
This means that the call to require 'json'is unnecessary.

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

Thanks for providing these examples!

感谢您提供这些示例!

回答by Rawan-25

You could use JSON and net/http lib.. which is below:

您可以使用 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"]