1

I have written the following code:

require 'rubygems'
require 'json'
require 'net/http'
require 'uri'

uri = URI.parse('somejsonfile')
puts uri

http = Net::HTTP.new(uri.host, uri.port)
puts http
request = Net::HTTP::Get.new(uri.request_uri)
puts request
response = http.request(request)
puts response

when i print the above items i get the output:

#<Net::HTTP:0x26507b8>
#<Net::HTTP::Get:0x2650638>
#<Net::HTTPMovedPermanently:0x264f750>
#<Net::HTTP::Get:0x264e208>

somewhat like this.

how do i get the JSON objects in proper format.

Thanks.!

3
  • Maybe checking out what the methods you're using actually return would be useful. Commented Apr 1, 2014 at 13:32
  • return not helping either.! Commented Apr 1, 2014 at 13:35
  • ... I'm saying to look at the documentation for those methods so you know what you're actually doing. Commented Apr 1, 2014 at 13:36

2 Answers 2

1

Your question lacks a lot of informatin. I assume you have some HTTP service at 'somejsonfile' URL, which returns some json. If this is the case, and the URL is corret, the JSON should be in the response body.

Try parsing the body of the response:

my_json = JSON.parse(response.body)
Sign up to request clarification or add additional context in comments.

2 Comments

this helps but if i want to do for a certain link.. and that too https
you want to do what for a certain link? is the input a JSON or a URL?
0

Try using JSON.parse() instead of URI.parse

3 Comments

It called for a (JSON::ParserError) Error
Okay.. What format are you trying to achieve? Try your old method but change your puts lines to: puts http.to_s, puts request.to_s, puts response.to_s,
And i am trying to get the JSON object from the certain link and simply read it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.