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

时间:2023-02-06 08:20:26

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 app

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”] ......

3 个解决方案

#1


40  

JSON.load takes a source that is a String or IO object according to the documentation

根据文档,JSON.load接受一个String或IO对象的源

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-uri to 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

注意

open returns a StringIO object which responds to read returning 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.load or JSON.parse

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

#2


12  

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:

或宝石http派对:

require 'httparty'

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

#3


3  

By default, httparty already 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!

感谢您提供这些示例!

#1


40  

JSON.load takes a source that is a String or IO object according to the documentation

根据文档,JSON.load接受一个String或IO对象的源

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-uri to 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

注意

open returns a StringIO object which responds to read returning 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.load or JSON.parse

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

#2


12  

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:

或宝石http派对:

require 'httparty'

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

#3


3  

By default, httparty already 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!

感谢您提供这些示例!