Eric Eric - 1 year ago 60
Ruby Question

Ruby - Error when trying to parse HTML page using JSON.load(), error 795 or 757

I'm trying to parse a page using

, but unfortunately I always get the same error
JSON::ParserError: 795: unexpected token at '{"

It actually works when I don't do the loop. If I set a constant for {page}, let's say 1, it does work. In the loop, it fails.

I assume, there is an error because of the two "{" as I get two Hashes, one for page 1 and one for page 2. Am I right?

Here is my code:

@dl_page = []
(1..2).each do |page|
puts "Loading page #{page}..."
@dl_page << [ `(curl --url "{page}&sort=joined" -H "X-Requested-With: XMLHttpRequest")` ]
@result = "#{@dl_page.join}"

html = JSON.load(@result)["html"]

Many thanks for your help!

Answer Source

In your code @result looks like: "{"html": "<...>"}{"html": "<...>"}". It is not valid JSON.

If you need to concatenate html from both requests:

@dl_page = (1..2).map do |page|
  puts "Loading page #{page}..."
  `(curl --url "{page}&sort=joined" -H "X-Requested-With: XMLHttpRequest")`

@result = "[#{@dl_page.join(',')}]"

html = JSON.load(@result)
# => [{"html": "<...>"}, {"html": "<...>"}]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download